如何强制向量不可变?

我想限制向量不变。在下面的代码中,当我为每个循环使用引用并增加每个值时,向量中也反映了相同的内容。但我想避免。

#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};    

  • Or use `const` keyword after `vector<int>`. `vector<int> const port = {8,0,8,0};`

以上是如何强制向量不可变?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>