I am trying to index a char in a string from a list of strings.
if stringV is a vector this works:
vector<string> stringV;
for (int i = 0; i < stringL.size(); i++) {
if(stringV[i][0] == 'a')
cout << stringV[i] << endl;
}
but if stringL is a list it does not work:
list<string> stringL;
for (list<string>::iterator it = stringL.begin(); it != stringL.end(); it++) {
if (*it[0] == 'a')
cout << *it << endl;
}
neither does this work:
for (list<string>::iterator it = stringL.begin(); it != stringL.end(); it++) {
if (*it.at(0) == 'a')
cout << *it << endl;
}
These are my header files:
include <string>
include <list>
include <vector>
The reason I am using a list instead of a vector is so that I can insert middle elements more efficiently. Any suggestions on how I index a character from a string in a list?
答案 0 :(得分:4)
Instead of *it.at(0)
use (*it).at(0)
or it->at(0)
.
The expression *it.at(0)
parses as *(it.at(0))
, which is meaningless: a char
can't be dereferenced.