set up a const std::vector

I'd like to read some data (the number of elements is not known at compile time) from a file and store them in a vector.
I also like the vector to be const, so that I am sure my data will not be overwritten by operations I mean to do with them.

What is the correct technique I should use to push_back() data in a const vector?
Const is by definition constant as in "does not change".

How about:
1
2
3
4
int foo;
std::cin >> foo; // modify non-const object
const int bar = foo; // initialize a const object
// use bar 
Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>
#include <string>
#include <fstream>

std::vector<std::string> get_tokens( std::ifstream stm ) 
{
    std::vector<std::string> vec ;

    std::string token ;
    while( stm >> token ) vec.push_back(token) ;

    return vec ;
}

int main()
{
    const std::vector<std::string> tokens = get_tokens( std::ifstream(__FILE__) ) ;
    std::cout << tokens.size() << " tokens were read\n" ;
}

http://coliru.stacked-crooked.com/a/e07ebe80428af57d
Topic archived. No new replies allowed.