473,598 Members | 2,844 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std:: vector push_back a struct

CPP question: if i had a struct like "struct str { int a; int b };"
and a vector "std::vecto r < 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 11599
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::vecto r < 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<st rtest;", 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::vecto r < 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::vecto r < 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...@earth link.netwrote:
jmsanchezdiaz <jmsanchezd...@ gmail.comwrote:
CPP question: if i had a struct like "struct str { int a; int b };"
and a vector "std::vecto r < 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:

addPacketToSend Buffer( CONTROL_HV, 0, sizeof(T_Contro lHV), (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*)&(h2pcno va->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...@earth link.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<cha r>) 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:
addPacketToSend Buffer( CONTROL_HV, 0, sizeof(T_Contro lHV),(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*)&(h2pcno va->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_CONCE PT_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 objektorientier ter Datenverarbeitu ng
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...@earth link.netwrote:


jmsanchezdiaz <jmsanchezd...@ gmail.comwrote:
CPP question: if i had a struct like "struct str { int a; int b };"
and a vector "std::vecto r < 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:

addPacketToSend Buffer( CONTROL_HV, 0, sizeof(T_Contro lHV), (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*)&(h2pcno va->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_fot oobjects in it yet;

In simpler terms:
class Z {...};
vector<Zvector_ zeds;
vector_zeds[0].some_method(); // WRONG - NO SUCH ELEMENT
vector_zeds.pus h_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
17127
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 should one use shared_ptr for every cases ? Thanks in advance for your remarks ! SerGioGio
4
5554
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. Thanks in advance. /usr/include/c++/3.2.3/bits/stl_algo.h: In function `_OutputIter std::remove_copy_if(_InputIter, _InputIter, _OutputIter, _Predicate) ': /usr/include/c++/3.2.3/bits/stl_algo.h:1085: instantiated from `_ForwardIter...
7
3538
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 there is a huge overhead of using this function, which comes from copying the function object, i.e., the object that compares the elements of the vector, which is passed to the function not by reference but by value. Now, at the end of this mail...
5
8134
by: Simon Elliott | last post by:
I'd like to do something along these lines: struct foo { int i1_; int i2_; }; struct bar {
5
2406
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 necessary. The function below is supposed to remove this It seems to work unitl the last point is removed. It seems to have something to do with the fact that vp->end() is a redundant point. THe test case is code so that "a" is the initial point,...
24
2912
by: simon | last post by:
Hi, First some background. I have a structure, struct sFileData { char*sSomeString1; char*sSomeString2;
20
5092
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 widgets classes, like buttons, images, slider, etc... Each class has a draw() method.
18
7199
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 <bitset> using std::vector;
6
3252
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 <vector> #include <algorithm> template <typename T> struct delete_ptr {
0
7894
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8284
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8392
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8262
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2410
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1500
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.