How can I enforce a vector to be immutable?

Viewed 159

I want to restrict vector to be unchanged. In following code when i use reference for each loop and increment each value, same is reflected in the vector. But I want to avoid.

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> port = {8, 0, 8, 0};    
    for (auto &digit: port){
        digit++;
        std::cout << digit << std::endl;
    }
}
1 Answers

use const keyword before vector<int>.

const vector< int> port = {8,0,8,0};    
Related