how to loop over vector of vector ?

Hi,
I've written the following in cpp, I have a table of strings streamTable, and a nested vector that contains strings that I wish to loop through. my aim is to loop over each element of streamTable , and compare it with the first elements of the vector (A1,B1,C1,D1,E1) then go to second vector element (A2,B2,C2,D2,E2) and so on, as montioned in the expected results below.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  #include <iostream>
#include <string>
#include <vector>
using namespace std;

using T = vector<string>;
using VT = vector<T>;

int main()
{
    cout << "Hello World!" << endl;
     
    T streamTable;
    streamTable.push_back("S1");
    streamTable.push_back("A1");
    streamTable.push_back("A2");
    streamTable.push_back("A4");
    streamTable.push_back("A4");
    streamTable.push_back("A5");
    streamTable.push_back("A6");
    streamTable.push_back("S2");
    streamTable.push_back("S3");
    streamTable.push_back("B1");
    streamTable.push_back("B2");
    streamTable.push_back("B4");
    streamTable.push_back("B4");
    streamTable.push_back("B5");
    streamTable.push_back("B6");
    streamTable.push_back("S4");
    streamTable.push_back("S5");
    streamTable.push_back("S6");
    streamTable.push_back("C1");
    streamTable.push_back("C2");
    streamTable.push_back("C3");
    streamTable.push_back("C4");
    streamTable.push_back("C5");
    streamTable.push_back("C6");
    streamTable.push_back("S7");
    streamTable.push_back("S8");
    streamTable.push_back("S9");
    streamTable.push_back("S10");
    streamTable.push_back("D1");
    streamTable.push_back("D2");
    streamTable.push_back("D3");
    streamTable.push_back("D4");
    streamTable.push_back("D5");
    streamTable.push_back("D6");
    streamTable.push_back("S11");
    streamTable.push_back("E1");
    streamTable.push_back("E2");
    streamTable.push_back("E3");
    streamTable.push_back("E4");
    streamTable.push_back("E5");
    streamTable.push_back("E6");
    streamTable.push_back("S12");
    streamTable.push_back("S13");
    
    VT kws;
    string a1 = "A2", a2 = "A4", a3 = "A6";
    T A;
    A.push_back(a1);
    A.push_back(a2);
    A.push_back(a3);

    string b1 = "B2", b2 = "B4", b3 = "B6";
    T B;
    B.push_back(b1);
    B.push_back(b2);
    B.push_back(b3);

    string c1 = "C2", c2 = "C4", c3 = "C6";
    T C;
    C.push_back(c1);
    C.push_back(c2);
    C.push_back(c3);

    string e1 = "E2", e2 = "E4", e3 = "E6";
    T E;
    E.push_back(e1);
    E.push_back(e2);
    E.push_back(e3);

    string d1 = "D2", d2 = "D4", d3 = "D6";
    T D;
    D.push_back(d1);
    D.push_back(d2);
    D.push_back(d3);

    kws.push_back(A);
    kws.push_back(B);
    kws.push_back(C);
    kws.push_back(D);
    kws.push_back(E);

    bool notFoundAtAll = true;
    int counter = 0;
    for (auto & i : streamTable){
        cout << i << endl;
        for(auto & j : kws) {

            cout << " => " <<j.at(counter) << endl;
            if(i == j.at(counter)){
             cout << j.at(counter) << endl;
             notFoundAtAll = false;
            }
            if(&j==&kws.back()){
                counter++;
                notFoundAtAll = false;
            }
            if(!notFoundAtAll){
                counter = 0;
            }
        }
    }

    return 0;
}


I got this results

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Hello World!
S1
 => A2
 => B2
 => C2
 => D2
 => E2
A1
 => A2
 => B2
 => C2
 => D2
 => E2
A2
 => A2
A2
 => B2
 => C2
 => D2
 => E2
A4
 => A2
 => B2
 => C2
 => D2
 => E2
A4
 => A2
 => B2
 => C2
 => D2
 => E2


The result that I expect is this one :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Hello World!
S1
 => A2
 => B2
 => C2
 => D2
 => E2
S1
 => A4
 => B4
 => C4
 => D4
 => E4
S1
 => A6
 => B6
 => C6
 => D6
 => E6
A1
 => A2
 => B2
 => C2
 => D2
 => E2
A1
 => A4
 => B4
 => C4
 => D4
 => E4
A1
 => A6
 => B6
 => C6
 => D6
 => E6


how it can be done ?
thank you in advance
Last edited on
This code is kinda hard to follow. You know you can use initializer lists for vectors, right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{

    vector<string> stringTable = 
    {
        "S1", "A1", "A2", "A4", "A4", "A5", "A6", 
        "S2", "S3", "B1", "B2", "B4", "B4", "B5", "B6", 
        "S4", "S5", "S6", "C1", "C2", "C3", "C4", "C5", "C6", 
        "S7", "S8", "S9", "S10", "D1", "D2", "D3", "D4", "D5", "D6", 
        "S11", "E1", "E2", "E3", "E4", "E5", "E6", 
        "S12", "S13"
    };

    vector<vector<string>> kws = 
    {
        { "A2", "A4", "A6" },
        { "B2", "B4", "B6" },
        { "C2", "C4", "C6" },
        { "D2", "D4", "D6" },
        { "E2", "E4", "E6" }
    };


    return 0;
}


Not sure of formatting for your "stringTable" -- what is the meaning of the mixture of S sprinkled among seemingly ordered A, B, C, D, E?

There's probably a way to redesign it further, using less variables and make things more clear, if you explain some of the reasoning for the items in your data structures.
Last edited on
You're code doesn't compile, so it's hard to help specifically.

Note than when you push_back an object, it is copied into the vector. It looks like you're comparing addresses, and I have no idea what ADs is.
@azdoud -- could change "kws" structure to achieve desired output. Perhaps you have confusion with row and column?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{

    vector<string> stringTable = 
    {
        "S1", "A1", "A2", "A4", "A4", "A5", "A6", 
        "S2", "S3", "B1", "B2", "B4", "B4", "B5", "B6", 
        "S4", "S5", "S6", "C1", "C2", "C3", "C4", "C5", "C6", 
        "S7", "S8", "S9", "S10", "D1", "D2", "D3", "D4", "D5", "D6", 
        "S11", "E1", "E2", "E3", "E4", "E5", "E6", 
        "S12", "S13"
    };

    vector<vector<string>> kws = 
    {
        { "A2", "B2", "C2", "D2", "E2" }, 
        { "A4", "B4", "C4", "D4", "E4" },
        { "A6", "B6", "C6", "D6", "E6" }
    };

    for (auto& s : stringTable)
    {        
        for (auto& row : kws)
        {
            cout << s << endl;
            for (auto& ele : row)
            {
                cout << "  =>  " << ele << endl;
            }
        }
    }

    return 0;
}


S1
  =>  A2
  =>  B2
  =>  C2
  =>  D2
  =>  E2
S1
  =>  A4
  =>  B4
  =>  C4
  =>  D4
  =>  E4
S1
  =>  A6
  =>  B6
  =>  C6
  =>  D6
  =>  E6
A1
  =>  A2
  =>  B2
  =>  C2
  =>  D2
  =>  E2
A1
  =>  A4
  =>  B4
  =>  C4
  =>  D4
  =>  E4
A1
  =>  A6
  =>  B6
  =>  C6
  =>  D6
  =>  E6
A2
  =>  A2
  =>  B2
  =>  C2
  =>  D2
  =>  E2
...
@icy1 If you're using a modern C++ compiler using one of the modern C++ standards you don't need the equal sign for your vector initialization, just use uniform initialization instead of the assignment.
1
2
3
4
5
    vector<vector<string>> kws {
        { "A2", "B2", "C2", "D2", "E2" }, 
        { "A4", "B4", "C4", "D4", "E4" },
        { "A6", "B6", "C6", "D6", "E6" }
    };


I am glad that you didn't continue using those horrible "using" statements in your examples. I hope the OP takes notice of the omission.



@Ganado :
You're code doesn't compile, so it's hard to help specifically.

Note than when you push_back an object, it is copied into the vector. It looks like you're comparing addresses, and I have no idea what ADs is.


I had corrected it, at line 106 if(&j==&kws.back()){
Last edited on
@icy1
could change "kws" structure to achieve desired output. Perhaps you have confusion with row and column?


no Sir I'm not confused, kws are a category of keywords I should you, A intel E are the categories
this is the C++ 11 version :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    vector<string> streamTable =
    {
        "S1", "A1", "A2", "A4", "A4", "A5", "A6",
        "S2", "S3", "B1", "B2", "B4", "B4", "B5", 
        "B6", "S4", "S5", "S6", "C1", "C2", "C3", 
        "C4", "C5", "C6", "S7", "S8", "S9", "S10", 
        "D1", "D2", "D3", "D4", "D5", "D6", "S11", 
        "E1", "E2", "E3", "E4", "E5", "E6", "S12", "S13"
    };

    vector<vector<string>> kws =
    {
        { "A2", "A4", "A6" },
        { "B2", "B4", "B6" },
        { "C2", "C4", "C6" },
        { "D2", "D4", "D6" },
        { "E2", "E4", "E6" }
    };

    
    int counter = 0;
    for (auto & i : streamTable){
        cout << i << endl;
        for(auto & j : kws) {

            cout << " => " <<j.at(counter) << endl;
            if(i == j.at(counter)){
             cout << j.at(counter) << endl;
    
            }
            if(&j==&kws.back()){
                counter++;
    
            }
            if(!notFoundAtAll){
                counter = 0;
            }
        }
    }

    return 0;
}


the problem I do not know how to get this result by respecting kws structure?
Last edited on
Still not understanding the role of your boolean variable "notFoundAtAll" if you're printing everything anyway (there is no searching/filtering happening).

To keep kws as it is, you'd iterate (not intuitively) over the columns first. This feels awkward and would be remedied by changing the form of the kws structure so that it's more natural for iteration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    vector<string> streamTable
    {
        "S1", "A1", "A2", "A4", "A4", "A5", "A6", 
        "S2", "S3", "B1", "B2", "B4", "B4", "B5", "B6", 
        "S4", "S5", "S6", "C1", "C2", "C3", "C4", "C5", "C6", 
        "S7", "S8", "S9", "S10", "D1", "D2", "D3", "D4", "D5", "D6", 
        "S11", "E1", "E2", "E3", "E4", "E5", "E6", 
        "S12", "S13"
    };
    
    vector<vector<string>> kws
    {
        { "A2", "A4", "A6" },
        { "B2", "B4", "B6" },
        { "C2", "C4", "C6" },
        { "D2", "D4", "D6" },
        { "E2", "E4", "E6" }
    };

    size_t total_columns = kws[0].size();
    for (auto& s : streamTable)
    {        
        for (int col=0; col<total_columns; ++col)
        {
            cout << s << endl; 
            for(auto& row : kws) 
            {
                cout << "  =>  " << row[col] << endl;
            }
        }
    }

    return 0;
}


Alternately, you could store a second copy of the table in a "transposed" form.

@az I have no clue what's going on. I still cannot compile your code as you have it. It seems that you are excluding relevant information, and in doing so making it hard for icy1 to know what you're actually trying to achieve. I would listen to icy1's advice and adjust from there.
Last edited on
Topic archived. No new replies allowed.