Benefits of vector over string?

后端 未结 7 1486
自闭症患者
自闭症患者 2020-12-14 09:52

This question is related to, but not quite the same as, this question.

Are there any benefits to using std::vector instead of std::str

相关标签:
7条回答
  • 2020-12-14 09:56

    As other answers mention, a vector could be marginally faster since it guarantees contiguous memory, even for small sizes, and doesn't add an extra null byte at the end. However, it is a lot simpler (code-wise) to concatenate two strings than it is to concatenate two vectors:

    Using vector:

    vector<char> a, b;
    // ...
    vector<char> c;
    c.insert(c.end(), a.begin(), a.end());
    c.insert(c.end(), b.begin(), b.end());
    

    Using string:

    string a, b;
    // ...
    string c = a + b;
    
    0 讨论(0)
  • 2020-12-14 10:00

    I think the only benefit you would gain from doing that would the ease of incrementing over the std::vector of characters, but even that can be done with an std::string.

    You have to remember that even though std::string seems like an object, it can be accessed like an array, so even accessing specific parts of a string can be done without the use of a std::vector

    0 讨论(0)
  • 2020-12-14 10:02

    Ideally one would use vector<unsigned char> to store arbitrary binary data - but I think you already knew this - as you referred to the old question.

    Other than that, using vector would definitely be more memory efficient, as string would add a terminating Nul character. Performance might also improve as the allocation mechanism is different for both - vectors guarantee contiguous memory!

    Besides that, using a string would not be correct, as callers/users might inadvertently invoking some of the string methods, which could be a disaster.

    0 讨论(0)
  • 2020-12-14 10:08

    Yes, vector<char> indeed does have more capabilities over string.

    Unlike string, vector<char> is guaranteed to preserve iterators, references, etc. during a swap operation. See: May std::vector make use of small buffer optimization?

    0 讨论(0)
  • 2020-12-14 10:12

    std::string on mingw uses 6 pointers and equal 24 bytes. std::vector is always 3 pointers = 12 bytes.

    But the fact that std::string uses more tools, makes me question if allocation, appending, inserting, removal is much more efficient with std::string.

    While something like a file buffer only requires a cstring, A text editor requires the most optimal string structure.

    0 讨论(0)
  • 2020-12-14 10:13

    Beyond readability, and ensuring another maintainer does not confuse the purpose of the std::string, there is not a lot of difference in function. You could of course consider char*/malloc as well, if efficiency is the only consideration.

    One potential issue I can think of:

    std::string defaults to storing <char>. If you later needed to handle another type (e.g. unsigned short) you might need to either:

    • Create your own typedef std::basic_string<unsigned short> (which moves you away from normal std::string handling)
    • Tentatively apply some reinterpret_cast logic in a setter.

    With a vector you could simply change the container to a std::vector<unsigned short>.

    0 讨论(0)
提交回复
热议问题