for example how can i read just the first character of the first string:
vector <string> vec= {"aa","ab","abc","efg"};
Thank you.
for example how can i read just the first character of the first string:
vector <string> vec= {"aa","ab","abc","efg"};
Thank you.
You can read it like this:
std::vector <std::string> vec = { "aa","ab","abc","efg" };
std::vector<char> result;
for (auto it = vec.begin(); it != vec.end(); ++it)
{
result.push_back((*it)[0]); // (*it)[0] == vec[i][0]
}
Now result is a vector of first elements of each string
A vector just stores stuff you put in it, in this case, std::strings. To access the first element of a vector, as you said, there are multiple ways of doing that. The easiest, if you are used to arrays, is just using []:
std::string myString = myVector[0]; // store the first string of myVector
The second question is, how to access a character of a string, again, there are multiple ways of doing that, but again you can use []:
char myChar = myString[0]; //store the first character of myString
Also tip: It is generally frowned upon to use using namespace std;