473,699 Members | 2,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::vector::as sign

what happens memory wise with this scenerio:

std::vector<int > vecInt(20, 999);

VecInt.assign(2 0, 0);
do all the 999 values get overwritten or does the vector reassign the
memory lso?

thanks
Jul 19 '05 #1
5 7387
scooter wrote:
what happens memory wise with this scenerio:

std::vector<int > vecInt(20, 999);

VecInt.assign(2 0, 0);
You have the parameters backwards.
do all the 999 values get overwritten or does the vector reassign the
memory lso?


Do you want the memory to be freed and reallocated?
If so, I know that

std::vector <int> vecInt (999, 20);

{
std::vector <int> temp ();
temp.swap (vecInt)
}

will do it. IIRC, vector::assign works identically to
constructing a new vector and using operator=, and in
that case (again IIRC) the memory is not guaranteed to
be released and reallocated.

Regards,
Buster.

Jul 19 '05 #2
Buster Copley wrote:
scooter wrote:
what happens memory wise with this scenerio:

std::vector<int > vecInt(20, 999);

VecInt.assign(2 0, 0);

You have the parameters backwards.


I'm sorry, I misunderstood the next sentence. You're fine.
do all the 999 values get overwritten or does the vector reassign the
memory lso?


I think the 999s get overwritten.
Regards,
Buster.

Jul 19 '05 #3
On Wed, 17 Sep 2003 20:18:39 +0100, Buster Copley <bu****@none.co m>
wrote:
scooter wrote:
what happens memory wise with this scenerio:

std::vector<int > vecInt(20, 999);

VecInt.assign(2 0, 0);
You have the parameters backwards.
do all the 999 values get overwritten or does the vector reassign the
memory lso?


Do you want the memory to be freed and reallocated?
If so, I know that

std::vector <int> vecInt (999, 20);

{
std::vector <int> temp ();


The above declares a function called temp, which isn't what you
wanted.
temp.swap (vecInt)
}


The canonical way to do this (which very few people seem to get right
for some reason), is:

std::vector<int >().swap(vecInt );

Tom
Jul 19 '05 #4

"tom_usenet " <to********@hot mail.com> wrote>
Do you want the memory to be freed and reallocated?
If so, I know that

std::vector <int> vecInt (999, 20);

{
std::vector <int> temp ();


The above declares a function called temp, which isn't what you
wanted.


Yes, silly of me. Thanks.
temp.swap (vecInt)
}


The canonical way to do this (which very few people seem to get right
for some reason), is:

std::vector<int >().swap(vecInt );


I have a question about that. I thought I read somewhere that a
temporary is not modifiable. Isn't the result of the expression
'std::vector <int> ()' (when it is an expression...) a temporary?
So what am I missing?

Regards,
Buster.
Jul 19 '05 #5
On Thu, 18 Sep 2003 14:53:41 +0100, "Buster" <no***@nowhere. com>
wrote:
I have a question about that. I thought I read somewhere that a
temporary is not modifiable. Isn't the result of the expression
'std::vector <int> ()' (when it is an expression...) a temporary?
So what am I missing?


Temporaries are modifiable, it's just that you can't bind them to
non-const references. e.g. this is illegal:

vecInt.swap(std ::vector<int>() );

Here a temporary is passed as the argument to swap. This argument is a
non-const reference, so the code is illegal.

So, to summarize, you can call member functions (const or non-const)
on non-const temporaries, but you can't bind them to non-const
references. This is why the standard swap technique works, but some
alternatives don't.

Tom
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
12241
by: enzo | last post by:
hi all, i don't understand what's wrong: 1) std::vector<double> p(10); doesn't compile:
26
10721
by: BCC | last post by:
Hi, A colleague has some code like this: class CMyObject { // Bunch of Member functions } class CMyObjectList: public std::vector<CMyObject> {
32
69678
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: std::vector<int> fun(){ //build the vector v; return v; }
9
8895
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter; Any ideas why is tihs happening? The code is as follows: template <class T> struct StdVectorStorage { std::vector<T>* _storage;
6
5644
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. char temp; std::vector<unsigned charmmessage; while (!done){
11
2429
by: nandor.sieben | last post by:
I am trying to replace template < class T void set2vector (const set < T &s, vector < T &v) { typename set < T >::iterator it; for (it = s.begin (); it != s.end (); it++) { v.push_back (*it); } }
16
26022
by: JDT | last post by:
Hi, Can someone show me how to set any integer (or float) in an std::vector as zero in a way other than using a for loop? Can we apply memset() or ZeroMemory() to the vector? Thanks for your help. JD
29
3417
by: stephen b | last post by:
Hi all, personally I'd love to be able to do something like this: vector<intv; v.assign(1, 2, 5, 9, 8, 7) etc without having to manually add elements by doing v = 1, v = 2 .. etc. it would make for much more readable code that is faster to write in some situations. I've not seen this feature documented anywhere
19
10748
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is there an existing adapter? Thanks, Daniel.
0
8687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8617
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
9174
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
9035
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...
0
8884
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...
0
7751
agi2029
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...
1
6534
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
4629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2347
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.