如何对列表c++中的字符串中的字符进行索引
How to index character in string from a list c++
我正试图从字符串列表中为字符串中的一个字符编制索引。
如果stringV是一个矢量,那么它就起作用:
vector<string> stringV;
for (int i = 0; i < stringL.size(); i++) {
if(stringV[i][0] == 'a')
cout << stringV[i] << endl;
}
但如果字符串L是一个列表,它就不起作用:
list<string> stringL;
for (list<string>::iterator it = stringL.begin(); it != stringL.end(); it++) {
if (*it[0] == 'a')
cout << *it << endl;
}
这也不起作用:
for (list<string>::iterator it = stringL.begin(); it != stringL.end(); it++) {
if (*it.at(0) == 'a')
cout << *it << endl;
}
这些是我的头文件:
include <string>
include <list>
include <vector>
我使用列表而不是向量的原因是为了更有效地插入中间元素。关于如何从列表中的字符串中索引字符,有什么建议吗?
使用(*it).at(0)
或it->at(0)
代替*it.at(0)
。
表达式*it.at(0)
解析为*(it.at(0))
,这是没有意义的:char
不能被取消引用。
相关文章:
- 将转换字符键入 int 以用作向量C++的索引
- 查找字符在两个索引之间出现的次数
- *无符号字符中的索引值
- C++ char 类查找字符索引
- int 数组,但索引是字符?
- 为什么在 c++ 中索引字符串会发出隐式转换警告?
- 根据字符获取数组的索引
- CPP:如何获取字符的最后一个索引
- 如何计算 3d 数组中的索引值在内存中的位置?如何计算字符**中的索引值在内存中的位置?
- 将特定索引处的字符数组内容与字符文字 - cpp 进行比较
- 为字符串中的每个字符(不是字符位置!)分配唯一的索引
- 通过字符串的字符对整数数组进行索引
- 从给定索引返回子字符字符串的函数
- 在C 字符串中,为什么在最后一个字符之后,通过索引和()访问索引时行为是不同的
- 是否可以使用字符串或字符索引数组
- 如何使该程序在数组中打印搜索字符的索引,否则将找不到它
- 如何在 C++ 的字符串中打印从一个索引到另一个索引的字符范围
- 为什么在使用字符索引数组时会出现这种不同的行为
- 从 1 中创建新的字符 * 作为参数,元素位于 2 个索引之间
- 字符串下标超出范围.我不知道如何使用字符索引数组,所以我使用了(无符号整数),但它不起作用