2

I need to create array of string which one will be the more efficient.

1.std::array<std::string, 3>arr  {"aa", "bb", "cc"};
2.std::vector<std::string> arr = {"aa","bb","cc"};
3.string arr[3]                = {"aa", "bb", "cc"};

Note : All the string(aa,bb,cc) in my case is fixed during initialised time. please suggested any other way if any.

3
  • Do you ever need to resize the array? Pass it as an argument? Commented Aug 9, 2018 at 1:59
  • No this is fixed during the complie time . but i need to search the string from the fixed array i.e in our case aa,bb,cc and return to search to caller. Commented Aug 9, 2018 at 2:04
  • @newcpp if the size is known at compile time and never changed at runtime, use std::array. Otherwise, use std::vector. Commented Aug 9, 2018 at 2:41

2 Answers 2

4

std::array<std::string, 3> arr{"aa", "bb", "cc"}; and string arr[3] = {"aa", "bb", "cc"}; should be equally performant. std::array bakes the array size into the type so it doesn't degrade to a pointer with unknown size when passed around (just make sure it's passed around by reference to avoid copies), and has more C++ utility methods, but otherwise has the same performance and behavior characteristics as C arrays, so it's probably the way to go.

If you can use C++14, a very small improvement could be made by making the strings std::string literals, which allows them to be converted from C-style strings to std::string slightly more efficiently (the size is baked in at compile time, so it can use the known length constructor, rather than the one that must first scan for the NUL terminator).

using namespace std::string_literals;

std::array<std::string, 3> arr{"aa"s, "bb"s, "cc"s};  // s suffix makes them string literals
Sign up to request clarification or add additional context in comments.

2 Comments

I am using c++11 so i can't use std::string_literals; any alternative for it on c++11 any good way other than {std::string(aa),..,..}
@newcpp: The literals are mostly equivalent to using the pointer+length constructor for std::string, so "aa"s could be written more verbosely as a std::string("aa", 2) (but it means hardcoding in the lengths of each string). std::string("aa") gains you nothing at all (it's just writing explicitly what happens implicitly when you assign a char* to a std::string).
2

I will go for 3rd one. Simple and there is no compile time or run time overhead off std::array or std::vector

string arr[3]                = {"aa", "bb", "cc"};

1 Comment

std::array should have no overhead beyond plain C style arrays, at least at runtime. It might slightly slow compilation given the cost of realizing the template at compile time, but other than that, it should behave equivalently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.