How would I find and replace chars inside a 2d vector of chars? c++

Viewed 483

Lets say I have a 2d array of chars that looks like:

ooooooooo
ooooooooo
ooooooooo
ooooxoooo
ooooooooo
ooooooooo
ooooooooo

lets say i want to replace all the o's to be I instead so it would look like this:

IIIIIIIII
IIIIIIIII
IIIIIIIII
IIIIxIIII
IIIIIIIII
IIIIIIIII
IIIIIIIII

the current way im getting input from the user is using the command line:

int main(int argc, char* argv[]) {

This is what ive come up with so far:

void replacee(vector<vector<char>> &vec, char oldd, char neww) {
    for(vector<char> v:vec)
    // change
    {
        for(char c:v) 
         if (c == oldd) {
            c == neww;
         }
    }
}

Ps. Sorry if this is a simple solution, im new to c++ and ive looked up ways other people have done it but they have always had a vector of Strings instead of char's.

4 Answers

Your outer loop needs to take a reference to the inner vector. Without a reference the inner loop only modifies a copy of the vector. One suggestion, use a standard library algorithm instead of a loop to modify the values.

#include <algorithm>

void replacee(vector<vector<char>> &vec, char oldd, char neww)
{
    for (vector<char> &v : vec) // reference to innver vector
    {
        replace(v.begin(), v.end(), oldd, neww); // standard library algorithm
    }
}

Note the critical difference between = and ==. The expression c == oldd is a test for equality. That is, it returns true if c and oldd have the same value, and false otherwise. On the other hand, an expression like c = neww is an assignment, which means that the value of neww is copied into the variable c.

Additionally, in your for loops, you should loop over elements by reference if you want to make lasting changes.

for(vector<char> v : vec){ /* v is a copy, changing it will leave no effect */ }

for(vector<char>& v : vec){ /* v is a reference, changes to it will be visible outside */ }

Try this fix:

for(vector<char>& v : vec){
    for(char& c : v){
        if (c == oldd) { // only enter the clause if c and old are equal
            // c == neww; <- this does nothing, it returns a boolean value that never gets used
            c = neww; // <- this assigns the new value
        }
    }
}

Hope that helps! Happy coding

You're super close! You only have two mistakes.

  1. This one's pretty silly, but it looks like you accidentally put an extra equal's sign in the assignment of the new character. Change c == neww; (which compares them and does nothing) to c = neww;.
  2. This is a little more nuanced, but you'll need to change your loops to use reference variables. Right now, when you loop, you're dealing with a copy of each row, and c is a copy of each value in each copied row. This is as simple as adding two &'s in your loops.

When all is said an done and you fix your indenting and make your formatting stick to either braces on the same line or the next line, things should look like:

void replacee(vector<vector<char>> &vec, char oldd, char neww) {
    for(vector<char> &v:vec) {
        for(char &c:v) {
            if (c == oldd) { c = neww; }
        }
    }
}

See it work here: ideone

void replace(vector<vector<char>> &vec, char oldd, char neww) {
    for(auto v : vec)
    {
        for(auto c : v) 
         if (c == oldd) {
            c = neww;
         }
    }
}

Here you go. C++ allows you to use auto in a foreach loop and etc. and it is super neat.

Related