Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie question on vectors

Status
Not open for further replies.

SirNitro

Programmer
May 20, 2003
2
US
I'm not having much luck with this one thing I'm trying to do with vectors (apvectors specifically. I've been using them since I first started). Basically, I have a pointer to a struct. One of the struct's members is a vector of pointers to another type of struct. What I want to do is access the members of the structs in the vector, but I can't figure out how, and I can't find much help on it. Here's basically what it's like:
Code:
struct somedata
{
	int x = 5;
	char y = '?';
};

typedef somedata *p_somedata;

struct example
{
	apvector<p_somedata> thisVector(;
	//other data members
};

typedef example *p_ex;
p_ex linkedList; 

void getMembers(p_ex &linkedList;
{
	int i = 0;
	p_somedata c;
	c = linkedList;
	
	//now, here's where my problem is
	cout << c->thisVector //now what?
}
The compiler wants me to do something like
Code:
	cout << c->thisVector::x[i];
but that doesn't seem right. I thought :: was only used for classes and the like, not vectors and their members. Plus, with
Code:
[i]
at the end, I think it would only refer to the x, not the vector. I'm probably overlooking something simple, but I don't know what, so could anyone help me out? I'd greatly apprectiate it.
 
Here's one approach:

vector.at(?) will return the vector's object at '?' position. After such a declaration, you treat the returned object as you would a normal instantiation of an object. For instance, a vector of vectors might look like this:

int main()
{
std::vector< std::vector<int> > v;
std::vector< int > subV1;
std::vector< int > subV2;

subV1.push_back( 5 );
subV1.push_back( 7 );

subV2.push_back( 9 );
subV2.push_back( 3 );


v.push_back( subV1 );
v.push_back( subV2 );

//print the first value of [at(0)] of the second vector [v.at(1)]
//v.at(1) returns subV1, so v.at( 1 ).at( 0 ) == subV2.at( 0 )

cout << v.at( 1 ).at( 0 ) << endl;

return 0;
}


For a vector of structs, use .at() and treat the struct as you normally would with the dot or arrow operator.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top