错误:具有索引或迭代字符串的"<"标记之前的预期初始值设定项
error: expected initializer before ‘<’ token with indexed or iterated string
这对我来说是最奇怪的错误。
g++ -Wall -g -std=c++11 *.cpp -o lab2
lab2.cpp: In function ‘void toAlpha(std::string&)’:
lab2.cpp:18:19: error: expected initializer before ‘<’ token
for(int i = 0, i < str.length(), ++i){
^
lab2.cpp:18:19: error: expected ‘;’ before ‘<’ token
lab2.cpp:18:19: error: expected primary-expression before ‘<’ token
lab2.cpp:18:38: error: expected ‘;’ before ‘)’ token
for(int i = 0, i < str.length(), ++i){
^
从我读过的内容中。此错误通常来自上述行之上的东西。但是,它几乎是代码中的第一个功能。也许您可以帮助看看我的眼睛无法。
fyi函数的目的是将所有非阿尔法字符转换为空格。
无论我是通过索引还是迭代器访问。
这是代码:
#include <map>
#include <iostream>
#include <set>
#include <fstream>
#include <algorithm>
#include <list>
#include <cctype>
#include <sstream>
#include "print.h"
using namespace std;
typedef map<string,list<int>> WORDMAP;
/* makes symbols turn into spaces */
void toAlpha(string& str){
for(int i = 0, i < str.length(), ++i){
if(!isalpha(str[i])){
str[i] = ' ';
}
}
}
您需要在循环语句中使用;
。
这是由于不正确的 for
循环语法
更改:
for(int i = 0, i < str.length(), ++i)
to:
for(int i = 0; i < str.length(); ++i)
// ^ ^
使用semicolons代替逗号:
void toAlpha(string& str){
for(int i = 0; i < str.length(); ++i){
if(!isalpha(str[i])){
str[i] = ' ';
}
}
}
for循环语法是这样的:
for(int i = 0; i < str.length(); ++i){
注意分号而不是逗号。
相关文章:
- 跟随整数索引列表的自定义类迭代器
- 为什么 min_element() 返回最小元素的索引,而不是迭代器?
- modify() 在迭代期间提升多索引键
- 在擦除或修改作为不同索引键的值时,boost::multi_index 迭代器是否无效?
- C++将向量迭代器转换为索引
- 使用索引与迭代器将向量迭代到倒数第二个元素
- 如何使equal_range迭代器在 Boost 多索引中按不同的索引排序?
- 仅在使用迭代器的某些索引上生成值
- 是否可以在C++中获得索引迭代器?
- 用于迭代不同类型的向量的索引类型
- 有没有办法创建一个花哨的迭代器和相应的新数组,以便检查每个索引的索引值的条件?
- 索引与迭代器 - 哪个更有效?
- C 迭代器比索引慢得多
- 存储Boost Multi_index索引迭代器
- 几对性能问题(一个较大的向量与较小的块向量),值得存储迭代索引以进行矢量访问
- 属性树常量迭代器的索引
- 矢量迭代器的索引
- Deque索引迭代器转换
- 仅对"specific"索引迭代嵌套"for"循环。C++
- 使用索引迭代STL容器的安全方式,避免使用锁