C/C++ C++ vector<vector<int> > problem

  • Thread starter Thread starter NeoDevin
  • Start date Start date
  • Tags Tags
    C++
Click For Summary
The discussion revolves around an issue with a vector of vectors in C++ where the user, Devin, encounters unexpected behavior when printing integer values. Devin initializes a vector of vectors named 'ph' and adds a vector 'p' containing three integers. However, when attempting to print the contents, the output includes a hexadecimal number instead of the expected integer values. This suggests a potential issue with memory access or incorrect indexing. Another participant points out that the user did not mention adding 'p' to 'ph' and questions the absence of a compile error when using the '<<' operator, which may indicate a custom definition. They recommend testing simpler code to clarify the behavior of the vector class. Ultimately, Devin resolves the issue independently and thanks the community for their support.
NeoDevin
Messages
334
Reaction score
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
Also, this is all implemented inside a class, if that has any effect on it.
 
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;
    }
}
 
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.
 
Never mind, I figured it out, Thanks
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
1
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K