C++ vector<vector<int> > problem

  • Context: C/C++ 
  • Thread starter Thread starter NeoDevin
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around a problem encountered with a C++ program involving a vector of vectors of integers. The issue pertains to unexpected output when printing the contents of the vector, specifically a mix of expected integers and an unexpected hexadecimal value.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a problem with printing values from a vector of vectors, where the output includes a hexadecimal number instead of the expected integer.
  • Another participant questions the completeness of the initial description, suggesting that the participant may have forgotten to mention adding the vector to the vector of vectors.
  • Concerns are raised about the potential for a compile error due to the use of the << operator, which may not be defined for the vector of vectors without a custom definition.
  • A suggestion is made to test simpler code to clarify the behavior of the vector class.
  • The original poster later indicates they have resolved the issue independently.

Areas of Agreement / Disagreement

Participants do not reach a consensus, as the original poster resolves their issue without detailing the solution, leaving the discussion open-ended regarding the initial problem.

Contextual Notes

There are potential limitations in the original description, including missing details about the class context and the handling of the vector of vectors. The discussion does not clarify whether the output issue was due to a coding error or a misunderstanding of vector behavior.

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
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K