Adn*_*ib 2 c++ vector mutable immutability
我想限制向量不变。在下面的代码中,当我为每个循环使用引用并增加每个值时,向量中也反映了相同的内容。但我想避免。
#include <vector>
#include <iostream>
int main()
{
std::vector<int> port = {8, 0, 8, 0};
for (auto &digit: port){
digit++;
std::cout << digit << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
const之前使用关键字vector<int>。
const vector< int> port = {8,0,8,0};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
101 次 |
| 最近记录: |