I am trying to make a vector extended header. One of the functions sets a vector to the console input (cin). It uses the following code:
vector<char> cveccin(){
string cinval;
cin>>cinval;
vector<char> creader;
for (int i=0; i<cinval.size(); i++) {
creader[i]=cinval[i];
}
return creader;
}
I use this function in a test, and it gives me EXC_BAD_ACCESS. What’s going wrong here?
The problem is that your vector is size zero, and using [] won’t make it any bigger.
There’s a one-liner that does what you want.
That code creates the vector of the right size, copies the cinval string to it, and returns it from the function, all in one line. Ain’t C++ marvellous!