By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,572 Members | 1,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,572 IT Pros & Developers. It's quick & easy.

std:: vector push_back a struct

P: n/a
CPP question: if i had a struct like "struct str { int a; int b };"
and a vector "std::vector < str test;" and wanted to push_back a
struct, would i have to define the struct, fill it, and then push_back
it, or could i pushback the two ints directly somehow?

Thanks for all.
Dec 13 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Dec 13, 6:23 pm, jmsanchezdiaz <jmsanchezd...@gmail.comwrote:
CPP question: if i had a struct like "struct str { int a; int b };"
and a vector "std::vector < str test;" and wanted to push_back a
struct, would i have to define the struct, fill it, and then push_back
it, or could i pushback the two ints directly somehow?

Thanks for all.
Hi.

As far as I know, You have to define a struct, fill it and push_back
it.
If you defined the vector as "std::vector<strtest;", there is no way
to push_back
data members of struct str directly.

And it is not difficult to do so, give it a chance.

Y.w
Dec 13 '07 #2

P: n/a
LR
jmsanchezdiaz wrote:
CPP question: if i had a struct like "struct str { int a; int b };"
and a vector "std::vector < str test;" and wanted to push_back a
struct, would i have to define the struct, fill it, and then push_back
it, or could i pushback the two ints directly somehow?
You have to make an instance of the struct, yes. You can't push_back
ints onto a std::vector<str>.

But ctors don't always have to be called explicitly when you push.

for example, if str had a ctor like:
str::str(const char *p) : a(0), b(0) { ... }

then you could
test.push_back("hello");
test.push_back("99 44");

Or you can call a ctor as the argument to push_back. If you have this ctor:
str(const int aa, const int bb) : a(aa), b(bb) {}
then
test.push_back(str(5,4));
test.push_back(str(99,12));

What is it that you want to do?

LR
Dec 13 '07 #3

P: n/a
jmsanchezdiaz <jm***********@gmail.comwrote:
CPP question: if i had a struct like "struct str { int a; int b };"
and a vector "std::vector < str test;" and wanted to push_back a
struct, would i have to define the struct, fill it, and then push_back
it, or could i pushback the two ints directly somehow?
First choice:

struct str { int a; int b; str(int a, int b):a(a), b(b) { } };

int main() {
std::vector< str test;
test.push_back( str( 23, 44 ) );
}

Second choice (if you aren't allowed to change the struct.)

struct str { int a; int b; };

str make_str( int a, int b ) { str s; s.a = a; s.b = b; return s; }

int main() {
std::vector< str test;
test.push_back( make_str( 23, 44 ) );
}

Or if you really want to show off:

struct str { int a; int b; };

struct my_str : str { my_str( int a_, int b_ ) { a = a_; b = b_; } };

int main() {
std::vector< str test;
test.push_back( my_str( 23, 44 ) );
}
Dec 13 '07 #4

P: n/a
On 13 dic, 21:41, "Daniel T." <danie...@earthlink.netwrote:
jmsanchezdiaz <jmsanchezd...@gmail.comwrote:
CPP question: if i had a struct like "struct str { int a; int b };"
and a vector "std::vector < str test;" and wanted to push_back a
struct, would i have to define the struct, fill it, and then push_back
it, or could i pushback the two ints directly somehow?

First choice:

struct str { int a; int b; str(int a, int b):a(a), b(b) { } };

int main() {
std::vector< str test;
test.push_back( str( 23, 44 ) );

}

Second choice (if you aren't allowed to change the struct.)

struct str { int a; int b; };

str make_str( int a, int b ) { str s; s.a = a; s.b = b; return s; }

int main() {
std::vector< str test;
test.push_back( make_str( 23, 44 ) );

}

Or if you really want to show off:

struct str { int a; int b; };

struct my_str : str { my_str( int a_, int b_ ) { a = a_; b = b_; } };

int main() {
std::vector< str test;
test.push_back( my_str( 23, 44 ) );

}
I explain my question with more detail:

I have a:

typedef struct
{
CnovaMsgTypes id; // packet type identificator
char* data; //data of the structure

} data_foto;

vector< vector< data_foto vector_fotos;

And I want to access the fields of the struct for doing an assignement
in a case. I do this in a piece of my code:

switch(pfc[j].id)
{
case CONTROL_HV:

addPacketToSendBuffer( CONTROL_HV, 0, sizeof(T_ControlHV), (char *)
&h2pcnova->h2ig.cnt );
if (take_foto)
{
vector_fotos[minuto_actual][(int)(pfc[j].id)].id =
pfc[j].id;
vector_fotos[minuto_actual][(int)(pfc[j].id)].data
= (char*)&(h2pcnova->h2ig.cnt);
}

[...]

but when I debbug vector_fotos[minuto_actual] appears: "class
std::vector< data_foto, std::allocator< data_foto >&)0x0 Cannot
access"

What's the problem??

Thanks
Dec 18 '07 #5

P: n/a
On Dec 18, 12:59 pm, jmsanchezdiaz <jmsanchezd...@gmail.comwrote:
On 13 dic, 21:41, "Daniel T." <danie...@earthlink.netwrote:
jmsanchezdiaz <jmsanchezd...@gmail.comwrote:
I explain my question with more detail:
I have a:
typedef struct
{
CnovaMsgTypes id; // packet type identificator
char* data; //data of the structure
} data_foto;
In a header shared with C, no doubt. Otherwise, there's no need
for the typedef, and std::string (or std::vector<char>) would
doubtlessly be preferable to the char*. Also, you haven't shown
us the type of id. That could be important.
vector< vector< data_foto vector_fotos;
And I want to access the fields of the struct for doing an
assignement in a case. I do this in a piece of my code:
switch(pfc[j].id)
{
case CONTROL_HV:
addPacketToSendBuffer( CONTROL_HV, 0, sizeof(T_ControlHV),(char *)
&h2pcnova->h2ig.cnt );
if (take_foto)
{
vector_fotos[minuto_actual][(int)(pfc[j].id)].id =
pfc[j].id;
vector_fotos[minuto_actual][(int)(pfc[j].id)].data
= (char*)&(h2pcnova->h2ig.cnt);
}
[...]
but when I debbug vector_fotos[minuto_actual] appears: "class
std::vector< data_foto, std::allocator< data_foto >&)0x0 Cannot
access"
What's the problem??
Who knows? There's no where near enough information. If it's
really a case of your dereferencing a null pointer (which is
what the error message suggests), either you have a null pointer
somewhere yourself, or one of the vectors you access is in fact
empty.

Try compiling with debug turned on for the STL and use whatever
memory checkers you have. (With g++ under Linux, this would
mean "-D_GLIBCXX_CONCEPT_CHECKS -D_GLIBCXX_DEBUG
-D_GLIBCXX_DEBUG_PEDANTIC" and valgrind, for example.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 19 '07 #6

P: n/a
On Dec 18, 11:59 am, jmsanchezdiaz <jmsanchezd...@gmail.comwrote:
On 13 dic, 21:41, "Daniel T." <danie...@earthlink.netwrote:


jmsanchezdiaz <jmsanchezd...@gmail.comwrote:
CPP question: if i had a struct like "struct str { int a; int b };"
and a vector "std::vector < str test;" and wanted to push_back a
struct, would i have to define the struct, fill it, and then push_back
it, or could i pushback the two ints directly somehow?
First choice:
struct str { int a; int b; str(int a, int b):a(a), b(b) { } };
int main() {
std::vector< str test;
test.push_back( str( 23, 44 ) );
}
Second choice (if you aren't allowed to change the struct.)
struct str { int a; int b; };
str make_str( int a, int b ) { str s; s.a = a; s.b = b; return s; }
int main() {
std::vector< str test;
test.push_back( make_str( 23, 44 ) );
}
Or if you really want to show off:
struct str { int a; int b; };
struct my_str : str { my_str( int a_, int b_ ) { a = a_; b = b_; } };
int main() {
std::vector< str test;
test.push_back( my_str( 23, 44 ) );
}

I explain my question with more detail:

I have a:

typedef struct
{
CnovaMsgTypes id; // packet type identificator
char* data; //data of the structure

} data_foto;

vector< vector< data_foto vector_fotos;

And I want to access the fields of the struct for doing an assignement
in a case. I do this in a piece of my code:

switch(pfc[j].id)
{
case CONTROL_HV:

addPacketToSendBuffer( CONTROL_HV, 0, sizeof(T_ControlHV), (char *)
&h2pcnova->h2ig.cnt );
if (take_foto)
{
vector_fotos[minuto_actual][(int)(pfc[j].id)].id =
pfc[j].id;
vector_fotos[minuto_actual][(int)(pfc[j].id)].data
= (char*)&(h2pcnova->h2ig.cnt);
}

[...]

but when I debbug vector_fotos[minuto_actual] appears: "class
std::vector< data_foto, std::allocator< data_foto >&)0x0 Cannot
access"

What's the problem??
I'd guess that whereas you've declared a
vector< vector< data_foto vector_fotos;
you haven't actually put any vector<data_fotoobjects in it yet;

In simpler terms:
class Z {...};
vector<Zvector_zeds;
vector_zeds[0].some_method(); // WRONG - NO SUCH ELEMENT
vector_zeds.push_back(get_a_Z_from_somewhere());
vector_zeds[0].some_method(); // OK now

Dec 19 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.