Jagged Programmer
Jagged Programmer

Reputation: 31

Replace data in 2D vector C++

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

Upvotes: 2

Views: 8209

Answers (4)

Marcin Kolny
Marcin Kolny

Reputation: 633

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;
}

Upvotes: 1

swang
swang

Reputation: 5239

    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.

Upvotes: 0

Marcin
Marcin

Reputation: 238101

You should have :

        if (v2d[i][j] == '?'){
            v2d[i][j] = 'x';
        }

Upvotes: 0

Nasser Al-Shawwa
Nasser Al-Shawwa

Reputation: 3653

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

Upvotes: 0

Related Questions