I found something surprising with std::vector that I thought I'd ask about here to hopefully get some interesting answers.
The code below simply copies a string into a char vector and prints the contents of the vector in two ways.
#include <vector> #include <string> #include <iostream> int main() { std::string s("some string"); std::vector<char> v; v.reserve(s.size()+1); // copy using index operator for (std::size_t i=0; i<=s.size(); ++i) v[i] = s[i]; std::cout << "&v[0]: " << &v[0] << "\n"; std::cout << "begin/end: " << std::string(v.begin(), v.end()) << "\n"; // copy using push_back for (std::size_t i=0; i<=s.size(); ++i) v.push_back(s[i]); std::cout << "&v[0]: " << &v[0] << "\n"; std::cout << "begin/end: " << std::string(v.begin(), v.end()) << "\n"; return 0; } Building and running this yields:
$ g++ main.cpp -o v && ./v &v[0]: some string begin/end: &v[0]: some string begin/end: some string My expectation was that it would print the string correctly in both cases, but assigning character by character using the index operator doesn't print anything when later using begin() and end() iterators.
Why isn't end() updated when when using []? If this is intentional, what's the reason it's working like this?
Is there a reasonable explanation for this behaviour? :)
I've only tried this with gcc 4.6.1 so far.
resize, do usec_str()to guarantee a nullbyte at the end of the string. Or use some other means to guarantee it. Unless you're coding for C++11 and is absolutely certain that the compiler complies with C++11 on this (and also, that I'm not remembering the C++11 rules incorrectly). Cheers & hth.,s[s.size()]gives a reference to a null character. C++03 was a bit weirder, only requiring that for theconstoverload (which isn't used being used in this case), and fromc_str()as you say.