Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::vector< std::string > vec;
- std::string str( "Hello World!" ); // 1
- vec.push_back( str ); // 2
- std::string str2 = vec[ 0 ]; // 3
- std::string& str3 = vec[ 0 ]; // 4
- std::string* str4 = &vec[ 0 ]; // 5
- std::vector< std::string >::iterator it; // 6
- it = vec.begin(); // 7
- std::string str5 = ( * it ); // 8
- std::string& str6 = ( * it ); // 9
- std::string* str7 = &( * it ); // 10
- /*
- 1. The original string
- 2. A copy of 1 is put into vec
- 3. A copy of the string stored in vec (2)
- 4. A reference to the string in vec (2)
- 5. A pointer to the string in vec (2)
- 6. An empty iterator
- 7. An iterator pointing to the string in vec (2)
- 8. A copy of the string pointed to by it (7)
- 9. A reference to the string pointed to by it (7)
- 10. A pointer to the string pointed to by it (7)
- 'it' isn't an actual string. It's a thinly wrapped pointer.
- But, it doesn't implicitly convert to a pointer, so we can't
- just do std::string* ptr = it;.
- Instead, you'll need to convert it to a reference to a string
- (which is '( * it )'), and then get the address (which is
- '&( * it )'). If you try '&it', you are getting the address
- of the iterator, not the string. But in an iterator, the
- '( * it )' operator is overloaded to return a reference to
- the object it points to.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement