473,385 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

std:: vector push_back a struct

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
6 11574
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: SerGioGio | last post by:
Hello ! When a ref. counted smart pointer (template) class is available (like boost::shared_ptr), is there any (semantic) reason to still use std::auto_ptr rather than this smart pointer ? Or...
4
by: CupOfWater | last post by:
Hi, I'm getting some errors using remove_if that I can not fix at all. Let me paste my code and also the error here. I went on IRC and people were mostly no help. The compiler is g++ 3.2.3. ...
7
by: Ireneusz SZCZESNIAK | last post by:
I want to sort a vector with the std::sort function. There are two functions: one with two arguments, the other with three arguments. I am using the one with three arguments. I noticed that...
5
by: Simon Elliott | last post by:
I'd like to do something along these lines: struct foo { int i1_; int i2_; }; struct bar {
5
by: Billy Patton | last post by:
I have a polygon loaded into a vector. I need to remove redundant points. Here is an example line segment that shows redundant points a---------b--------c--------d Both b and c are not...
24
by: simon | last post by:
Hi, First some background. I have a structure, struct sFileData { char*sSomeString1; char*sSomeString2;
20
by: Manuel | last post by:
Hi. Before all, please excuse me for bad english and for very newbie questions. I hope to don't boring you. I'm trying to write a very simple GUI using openGL. So I'm writing some different...
18
by: ma740988 | last post by:
Trying to get more acclimated with the use of function objects. As part of my test, consider: # include <vector> # include <iostream> # include <algorithm> #include <stdexcept> #include...
6
by: lokchan | last post by:
i want to create a vector of pointer s.t. it can handle new and delete but also have std::vector interface can i implement by partial specialization and inherence like follow ? #include...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.