为什么这个 x 值在这个循环映射中没有增加?C++
Why isn't this x value increasing in this map for loop? c++
此循环的目的是查看二维向量,并计算第一列中出现值的频率。如果该值同时显示三次,则可以继续。如果没有,那么我想从向量中删除它所在的行。"it"迭代器将值存储为(value,frequency(。
不过,我现在不知道如何删除该行,我一直试图在第二个for循环中使用计数器"x",这样它就可以跟踪它所在的行,但当我通过调试器运行它时,x不会递增。最终发生的情况是,向量删除第一行,而不是使if语句为true的行。
为什么"x"不递增?有没有其他方法可以用来跟踪循环当前所在的行?
"data"是二维矢量。
for (int i = 0; i < data.size(); i++) // Process the matrix.
{
occurrences[data[i][0]]++;
}
for (map<string, unsigned int>::iterator it = occurrences.begin(); it != occurrences.end(); ++it)
{
int x = 0;
if ((*it).second < 3) // if the value doesn't show up three times, erase it
{
data.erase(data.begin() + x);
}
cout << setw(3) << (*it).first << " ---> " << (*it).second << endl; // show results
x++;
}
每个循环都将x重置回0。在循环外初始化它,它应该可以工作。
int x = 0;
您必须在for循环之外初始化x。如果在for循环中声明它,则每次都会将其设置为0。当前程序每次删除第一个元素,因为这里的x总是零:data.erase(data.begin() + x);
for (int i = 0; i < data.size(); i++) // Process the matrix.
{
occurrences[data[i][0]]++;
}
int x = 0;
for (map<string, unsigned int>::iterator it = occurrences.begin(); it != occurrences.end(); ++it)
{
if ((*it).second < 3) // if the value doesn't show up three times, erase it
{
data.erase(data.begin() + x);
}
cout << setw(3) << (*it).first << " ---> " << (*it).second << endl; // show results
x++;
}
相关文章:
- 数组索引的值没有增加
- 为什么我的代码在输出中增加了93天
- 有效地使用std::unordered_map来插入或增加键的值
- C++ 动态数组每次添加时将大小增加 1 - 错误
- 为什么要增加导致崩溃的指针
- 增加传递地址的值
- 如何增加以前由新运算符分配的 C++ std::list 数组的大小?
- 为什么"i"在循环的每次迭代中都没有增加?(C++)
- std::thread 增加 DLL 引用计数,从而防止卸载 DLL
- 如何在C++中为增加但记住删除先前对象的对象分配唯一标识符
- 将一对插入地图并增加计数?
- 整数类型应该显式转换(例如"int"到"无符号")还是只会增加混乱?
- 我的 c++ 应用程序中的运行时间从 0 增加到 60 太快了(例如一毫秒或一微秒)
- 为什么当通过 TCP 发送的消息速率增加时,请求-响应消息对的延迟会降低?
- 琐碎并发代码的吞吐量不会随着线程数量的增加而增加
- C++:功能在输出前自动使用50减/增加输入数
- 追加位以增加字符的大小
- 为什么在C++中增加指针后打印了一个值而不是 NULL/0?
- 内存增加和字符串流?
- 如何增加使用 CUDA 实现的 FLOPS