dimanche 22 février 2015

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




Aucun commentaire:

Enregistrer un commentaire