我想限制 vector 不变。在下面的代码中,当我为每个循环使用引用并递增每个值时,相同的内容反射(reflect)在 vector 中。但我想避免。
#include <vector>
#include <iostream>
int main()
{
std::vector<int> port = {8, 0, 8, 0};
for (auto &digit: port){
digit++;
std::cout << digit << std::endl;
}
}
最佳答案
使用const
vector<int>
之前的关键字.
const vector< int> port = {8,0,8,0};
关于c++ - 如何强制 vector 不可变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65265341/