std:: vector push_back a struct

J

jmsanchezdiaz

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.
 
T

The Last Ottoman

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<str> test;", 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
 
L

LR

jmsanchezdiaz said:
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
 
D

Daniel T.

jmsanchezdiaz said:
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 ) );
}
 
J

jmsanchezdiaz

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
 
J

James Kanze

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.)
 
T

tragomaskhalos

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_foto> objects in it yet;

In simpler terms:
class Z {...};
vector<Z> vector_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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top