I'm trying to understand what's wrong with this:
void getAllData( std::vector<uns igned char&char_array )
{
char_array.clea r();
char_array.rese rve( 11 );
char_array[0] = 'A'; //<----No error compilation, but no
effect!!
}
and then I call this function:
std::vector<uns igned charchar_array;
getAllData( char_array );
But if I do this:
void getAllData( std::vector<uns igned char&char_array )
{
char_array.clea r();
char_array.rese rve( 11 );
char_array.push _back('A'); //Works
}
It works good, but when I try to read some element with [] operator
doesn't works...
What's the correct way to deal with a vector inside a function???
PD: What's the diference between using & and * in function header's
parameter list? 3 1734
On Jan 11, 8:10 pm, clinisbut <clinis...@gmai l.comwrote:
I'm trying to understand what's wrong with this:
void getAllData( std::vector<uns igned char&char_array )
{
char_array.clea r();
char_array.rese rve( 11 );
the char_array is empty!
you can use char_array.resi ze(11); instead
>
char_array[0] = 'A'; //<----No error compilation, but no
effect!!
}
clinisbut wrote:
I'm trying to understand what's wrong with this:
void getAllData( std::vector<uns igned char&char_array )
{
char_array.clea r();
char_array.rese rve( 11 );
char_array[0] = 'A'; //<----No error compilation, but no effect!!
Actually, you have an out-of-bounds error and the code has undefined
behavior.
}
and then I call this function:
std::vector<uns igned charchar_array;
getAllData( char_array );
But if I do this:
void getAllData( std::vector<uns igned char&char_array )
{
char_array.clea r();
char_array.rese rve( 11 );
char_array.push _back('A'); //Works
}
This is a correct way to fill a vector.
It works good, but when I try to read some element with [] operator
doesn't works...
operator[] and the at() function only allow you to access vector elements
that are already stored in the container; calling them will not magically
enlarge the vector (as opposed to std::map, where this kind of magic
happens). Thus, you have to populate the vector _before_ you can access its
elements. The push_back() function is the way to go.
What's the correct way to deal with a vector inside a function???
That all of this happens inside a function is immaterial.
PD: What's the diference between using & and * in function header's
parameter list?
& denotes a reference parameter and * denotes a pointer.
Best
Kai-Uwe Bux
Oh... I think i confused reserve() with resize()...
thank you! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: luigi |
last post by:
Hi,
I am trying to speed up the perfomance of stl vector by
allocating/deallocating blocks of memory manually. one version of the
code crashes when I try to free the memory. The other version seem to
work. I would appreciate someone to comment on this.
Version 1 (crashes on deallocating)
#include <iostream>
|
by: Vasileios Zografos |
last post by:
Hello,
I have a function that generates some values (e.g. vertices in 2d space)
the number of which I dont know. So, it could generate 20 vertices, 100
vertices, or even 1 vertex.
void generateVals()
{
.....
//generate some values
|
by: Duncan |
last post by:
I need to populate a vector with the following struct details:\
struct group
{
string groupname;
int gid;
list<string> members;
};
the text file which this is to read from is in the following form:
|
by: John Black |
last post by:
Hi,
I am not familiar with for_each very well, suppoase I have a
vector<pair<unsigned int, unsigned int> > vec1 and the contents are
{<0x00000000, 0x000000FF>, <0x10000000, 0x2FFFFFFF>}
what I want is to create another vector, vector<int> vec2, which store
all the first 8 bit heads of the integer in vec1, for the above example,
0x00000000 & 0xFF000000 ==> 0x00000000,
|
by: Timothee Groleau |
last post by:
Hi all,
My name is Tim, I'm just getting started with C++ and this is my first post
to the group.
Is there a standard recommended approach to use a vector of pointers along
with <algorithm>?
To be specific, here is my situation, I have 2 classes A and B. Class A
contains a private vector of pointers to B objects and I need to sort this
| |
by: Sanjay Kumar |
last post by:
Folks,
I am getting back into C++ after a long time and I have
this simple question: How do pyou ass a STL container
like say a vector or a map (to and from a function) ?
function:
vector<string> tokenize(string s){
|
by: toton |
last post by:
Hi,
I want to have a vector like class with some additional functionality
(cosmetic one). So can I inherit a vector class to add the addition
function like,
CorresVector : public vector<Corres>{
public:
void addCorres(Corres& c); //it do little more than push_back
function.
}
|
by: alan.patterson5 |
last post by:
I have a vector of N values and a vector of N functors. I want to apply
each functor to its corresponding value. What would be the 'correct'
STL way to do this.
for_each takes on functor and applies it to every element.
So presumably transform is a better option but it also takes one
functor.
This is obviously easily solved by a variety of methods but I am
interested in how the STL would best solve this problem.
e.g.
|
by: themadme |
last post by:
im running code thats using threads, everytime i get this debug assertion failed box poping up.
It involves with my stl vector.
its saying that vector iterators incompatible.
the vector contains structs of data.
i have ten threads running, using the same function called "GetMemory()".
the function just looks through the function and swaps around the data in the vector list. The first ten containers in the vector are the Main...
|
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...
|
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,...
| |
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |