2

I am trying to search a 2D vector for a char, namely a '?' and replace it with 'x'.

I have no issues doing this task with a single vector but keep having issues with the a 2D vector implementation, see below for code.

#include <iostream>
#include <vector>
using namespace std;


int main()
{
        // An empty vector of vectors
        vector<vector<char> > v2d;

        // Create a vector with 5 elements
        vector<char> v2(5, '?');

        // Create a vector of 3 elements. 
        vector<vector<char> > v2d2(3, v2);

        // Print out the elements
        cout << "Before Vector Update" << endl;
        for (int i = 0; i < v2d2.size(); i++) {
            for (int j = 0; j < v2d2[i].size(); j++)
                cout << v2d2[i][j] << " ";
            cout << endl;
        }
        cout << "" << endl;

        /* Does not work as expected
        cout << "Vector Update" << endl;
        for (int i = 0; i < v2d2.size(); i++) {
            for (int j = 0; j < v2d2[i].size(); j++)
            {
                if (v2d[i] == '?');
                (v2d[i] = 'x');
            }
        }
        */

        cout << "" << endl;
        cout << "After Vector Update" << endl;
        for (int i = 0; i < v2d2.size(); i++) {
            for (int j = 0; j < v2d2[i].size(); j++)
                cout << v2d2[i][j] << " ";
            cout << endl;
        }

system("pause > NULL");
return 0;
}

I receive the error message below when I try and compile the code.

IntelliSense: no operator "==" matches these operands operand types are: std::vector>, std::allocator>>> == char Project3\Source.cpp 77 16 Project3

I believe it is an issue with the container in updating the proper row and column. Any help would be much appreciated.

Thank you

4 Answers4

1

There were a few bugs, please compare with your original code:

#include <iostream>
#include <vector>
using namespace std;


int main()
{
  // An empty vector of vectors
  vector<vector<char> > v2d;

  // Create a vector with 5 elements
  vector<char> v2(5, '?');

  // Create a vector of 3 elements. 
  vector<vector<char> > v2d2(3, v2);

  // Print out the elements
  cout << "Before Vector Update" << endl;
  for (int i = 0; i < v2d2.size(); i++) {
    for (int j = 0; j < v2d2[i].size(); j++)
      cout << v2d2[i][j] << " ";
    cout << endl;
  }

  for (int i = 0; i < v2d2.size(); i++) {
    for (int j = 0; j < v2d2[i].size(); j++)
      if (v2d2[i][j] == '?')
        v2d2[i][j] = 'x';
  }

  cout << "After Vector Update" << endl;
  for (int i = 0; i < v2d2.size(); i++) {
    for (int j = 0; j < v2d2[i].size(); j++)
      std::cout << v2d2[i][j] << " ";
    cout << endl;
  }


  return 0;
}
James Adkison
  • 9,412
  • 2
  • 29
  • 43
Marcin Kolny
  • 633
  • 7
  • 14
0

Take a look at your commented code:

cout << "Vector Update" << endl;
for (int i = 0; i < v2d2.size(); i++) {
    for (int j = 0; j < v2d2[i].size(); j++)
    {
        if (v2d[i] == '?'); // <------
        (v2d[i] = 'x');
    }
}

There is a semi-colon after the if statement, meaning that nothing will be executed if the statement is true. Rookie mistake.

Many consider it a bad practice not to always use parentheses with if-statements and other similar situations that require them, because of issues like this one

Community
  • 1
  • 1
Nasser Al-Shawwa
  • 3,573
  • 17
  • 27
0

You should have :

        if (v2d[i][j] == '?'){
            v2d[i][j] = 'x';
        }
Marcin
  • 215,873
  • 14
  • 235
  • 294
0
    cout << "Vector Update" << endl;
    for (int i = 0; i < v2d2.size(); i++) {
        for (int j = 0; j < v2d2[i].size(); j++)
        {
            if (v2d[i][j] == '?')
                v2d[i][j] = 'x';
        }
    }

You were not accessing the 2d vector correctly, should have used v2d[i][j] instead.

swang
  • 5,157
  • 5
  • 33
  • 55