简体   繁体   中英

How do I display the contents of a row in vector of vector from reverse in C++?

To display the contents of a vector of vector in C++, we use nested for loops. Something like this:

for (auto i = 0; i < x.size(); i++) {
        for (auto j = 0; j < x[i].size(); j++) 
            std :: cout << x[i][j] << " ";
        std :: cout << std :: endl;
    }

(Here x is a vector of vector.)

But say I want to display the contents of a row in a reverse manner and all the other rows in regular fashion. How would I do it? Should I reverse the row separately and print the vector? Or is there another way of doing it?

I'm not sure if this is what you want, but.

Either of this 2

this will reverse the order of i

for (int i = x.size() - 1; i >= 0; i--) {
        for (int j = 0; j < x[i].size(); j++){
            std :: cout << x[i][j] << " ";
        }
        std::cout << std::endl'
}

basically, what happen here is that, I started the i at the last index of the vector, to reverse the order

or

this will reverse the order of j

for(int i = 0; i < x.size(); ++i){
        for(int j = x[1].size() - 1; j >= 0; --j){
            std::cout << x[i][j] << " ";
        }
        std::cout << std::endl;
}

in here, i started the j at the last vector index of the vector.

Take note, that don't use auto for the initialization part of the for loop (in this case), because if you do auto i = x.size() - 1 you will get an unsigned long, and cause some trouble since this loop will stop only if the value is <= -1.

(1, 2, 3, 4, 5) and in row two I've: (21, 22, 23, 24, 25). I want to display the contents of row one in reverse manner (something like: 5, 4, 3, 2, 1) and the contents of row two in regular manner (21, 22, 23, 24, 25)

If this is what you want, then

bool notReverse = false;
    for(int i = 0; i < x.size(); ++i){
        int sIndex = notReverse ? 0 : x[0].size() - 1,   // starting index
            eIndex = notReverse ? x[0].size() : -1,      // last index
            mod_j  = notReverse ? 1 : -1;                // modifier
            
        for(int j = sIndex; j != eIndex; j+=mod_j){
            std::cout << x[i][j] << std::endl;
        }
        notReverse = true;
}

I've created some variables that will decide whether the value is gonna be reverse or not.

Although you can change the direction of the loop, if more instructions are working in the original direction (i++), you should rather just change the index:

auto y=x;
for (auto i = 0; i < x.size(); i++) {
    for (auto j = 0; j < x[i].size(); j++){
        std :: cout << x[i][j] << " ";//comment 1            
        std :: cout << y[(x.size()-1)-i][j] << " ";//comment 2
    }
    std :: cout << std :: endl;
}

comment 1: commands that are using the original indexing direction (0,1,2...)

comment 2: reverse indexing with the "i" iterator, starting in the last position (x.size()-1) ("-1" because the index of the fist element is zero). Please, take a look of this:

normal indexing (i++):
0,1,2,3,4,5
reverse indexing:
5,4,3,2,1,0
size:5 elements
size - (each index in normal indexing):
5-0,5-1,5-2,5-3,5-4,5-5
  5,  4,  3,  2,  1,  0    -> reverse indexing 

it's important to handle the indexing in both forms (changing the order while indexing and changing the order while looping through the elements), and I suggest you see also @Renz Aguirre's answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM