I am trying to use iterators to go through 2D vector and replace a specific symbol with another symbol.
If you take a look at the first code bellow you will notice that there is no problem to replace '!' with '+' (in standard vector). However, if I try the same approach with a 2D vector the compiler shows me the following error:
vec.at(col - row->begin()) = '+'; Compiler Error C2679 binary 'operator' : no operator found which takes a right-hand operand of type 'type' (or there is no acceptable conversion)
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
vector<char> myints{ '.', '.','!','.' };
vector<char>::iterator p;
p = find(begin(myints), end(myints), '!');
if (p != end(myints)) {
myints.at(p - begin(myints)) = '+';
}
for (auto i : myints) {
cout << i << " ";
}
return 0;
}
and the following:
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
void readVector(vector< vector<char> >& vec)
{
vector< vector<char> >::iterator row;
vector<char>::iterator col;
for (row = vec.begin(); row != vec.end(); ++row)
{
for (col = row->begin(); col != row->end(); ++col) {
if (*col == '!') {
vec.at(col - row->begin()) = '+';
}
}
}
}
int main()
{
// Initializing 2D vector "vect" with
// values
vector<vector<char> > vect{ { '.', '.', '.' },
{ '!', '.', '.' },
{ '!', '.', '!' } };
readVector(vect);
}
Thanks a lot in advance!