Home > Blockchain >  How to choose between `std::vector<char>` and `std::string`?
How to choose between `std::vector<char>` and `std::string`?

Time:06-09

What aspects need to be considered when making such a choice?

My two cents about this question:

1.The std::string is still valid even if there is a \0 in the middle of data stream.

2.std::string have many useful methods which could manipulate strings, whereas std::vector<char> does not provide.

3.If the data is just binary stream, it's better to choose std::vector<char> other than std::string.

CodePudding user response:

What aspects need to be considered when making such a choice?

What is the input data and what is it going to be used for in the future? That would be a big consideration.

If the input data is not going to be used as a string in the future, it may be clearer in the future if you used a container as specified in the comments e.g. std::vector<std::byte>.

  • Related