C++ vector<vector<int> > problem

  • C/C++
  • Thread starter NeoDevin
  • Start date
  • Tags
    C++
In summary, I'm having trouble figuring out why some of my integers are behaving crazy. Some of my functions seem to be evaluating the third number correctly, and give me the proper result, while others seem to evaluate it as zero, and printing it out gives me gibberish.
  • #1
NeoDevin
334
2
I'm having trouble figuring out why some of my integers are behaving crazy.

I have a vector of vectors, declared as

Code:
vector<vector<int> > ph;

In another function, I add a vector of three ints to it using

Code:
vector<int> p;
p.push_back(0); p.push_back(0); p.push_back(1);
ph.push_back(p);
And later I try to print out the contents by

Code:
int i;
for(i = 0; i < ph.size(); i++){
cout << ph[i][0] << " " << ph[i][1] << " " << ph[i][2] << endl;
} cout << endl;

and it prints out

0 0 0x633d50

where the last one seems to be a random hexadecimal number, or something.

Does anyone know why this might be happening? Some of my functions seem to be evaluating the third number correctly, and give me the proper result, while others seem to evaluate it as zero, and printing it out gives me gibberish.

Any help would be greatly appreciated.

Thanks in advance,
Devin.
 
Last edited:
Technology news on Phys.org
  • #2
Also, this is all implemented inside a class, if that has any effect on it.
 
  • #3
I think your description is incomplete. You declare a vector of vectors of integers named ph. In a separate function you declare vector of integers named p and add some values to it. These are different vectors. Did you forget to mention that you add p to ph? Later, you print three entries from the vector of vectors, but the << operator i not defined for this so you should get a compile error, yet you don't report any. Did you define one yourself?

You can also try simpler code and see if it can help you clarify how the vector class behaves:

Code:
#include <vector>
#include <iostream>

using namespace std;

int main() {
    vector< vector<int> > ph;

    vector<int> p;
    p.push_back(1);
    p.push_back(2);
    p.push_back(3);

    vector<int> q;
    q.push_back(10);
    q.push_back(20);
    q.push_back(30);
    q.push_back(40);

    ph.push_back(p);
    ph.push_back(q);

    for (vector< vector<int> >::size_type u = 0; u < ph.size(); u++) {
        for (vector<int>::size_type v = 0; v < ph[u].size(); v++) {
            cout << ph[u][v] << " ";
        }
        cout << endl;
    }
}
 
  • #4
Sorry, I made some typo's in my first post, I corrected them to what is in my program now. I've worked with vectors of vectors before, and never had this problem.
 
  • #5
Never mind, I figured it out, Thanks
 

1. What is a vector of vectors in C++?

A vector of vectors in C++ is a data structure that stores multiple vectors within a single vector. This allows for the creation of a 2-dimensional matrix or grid, where each row is a vector and the entire structure can be accessed using two indices.

2. How do I declare and initialize a vector of vectors in C++?

To declare and initialize a vector of vectors in C++, you can use the syntax: vector > myVectorOfVectors; then use the push_back() function to add individual vectors to the main vector. Alternatively, you can initialize the structure with a specific number of rows and columns using nested for loops.

3. How do I access elements in a vector of vectors in C++?

To access elements in a vector of vectors in C++, you use two indices to specify the row and column of the element you want to access. For example, myVectorOfVectors[1][2] would access the element in the second row and third column.

4. What are some common problems when working with vector of vectors in C++?

One common problem when working with vector of vectors in C++ is accessing out-of-bounds elements, which can result in a runtime error. Another issue is resizing or modifying the structure, which can cause unexpected behavior if not done carefully.

5. How do I efficiently iterate through a vector of vectors in C++?

To efficiently iterate through a vector of vectors in C++, you can use nested for loops or use iterators with the begin() and end() functions. Another approach is to use the for-each loop using the auto keyword to automatically infer the data type.

Similar threads

  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
3
Views
721
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
Back
Top