determining a length of a string inside a vector of strings

say I have a vector of strings:

vector <string> words = {"you", "are", "pretty"};

is there a way to calculate the length of the word "you" inside that vector?
Yes, they're std::strings, so they know their size, just ask them.

1
2
size_t len;
len = words[0].size();


Note that std::vector is not an associate container. The solution above assumes you know that "you" is in position 0 of the vector.

If you want an associate container, use std::set.
1
2
3
size_t len;
std::set<string> words = {"you", "are", "pretty"};
len = words["you"];

Note that std::set will not maintain the order of the words in the initialization list. Words in the set will be in alphabetical order.
Topic archived. No new replies allowed.