-3

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!

  • Please be more specific and in your question, and also put the exact error that the compiler throws. These programs intended to do two different things. The first going through the vector and searching for '+' and the second suppose to go over a two dimensions vector and put '10' instead of '!'. – Ido Jan 02 '21 at 15:17
  • Please be more specific when asking a question. The two code snippets do different things on different data structures. Please include the exact error message. Both code snippets are written in a "amateurish" style I would not recommend. Please find a good tutorial on C++ iterators. Simple replacements are usually done with STL algorithms. See, for example, https://www.geeksforgeeks.org/stdreplace-stdreplace_if-c/ – zkoza Jan 02 '21 at 15:30
  • The error is because `vec.at(col - row->begin())` returns the variable reference of `vector`, not `char&`. I guess what you need to do is replace `vec.at(col - row->begin())` with `*col`. – D-RAJ Jan 02 '21 at 15:41
  • Really thank you for the comments guys! I will try to be more specific and clear with my question next time! – Georgi Manov Jan 02 '21 at 15:50

1 Answers1

1

Modify offending line to

            row->at(col - row->begin()) = '+';

You've addressing the index of a row and not in the original vec.

On a side note your first program can be simplified to be used with many different containers with this change, no just random access containers.

   p = find(begin(myints), end(myints), '!');
   if (p != end(myints)) {
      *p = '+';
   }

The same goes to the offending line:

        if (*col == '!') {
            *col = '+';
         }
Bo R
  • 2,264
  • 1
  • 7
  • 16