473,803 Members | 3,167 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 7394
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
12262
by: enzo | last post by:
hi all, i don't understand what's wrong: 1) std::vector<double> p(10); doesn't compile:
26
10739
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
69701
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
8898
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
5655
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
2441
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
26035
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
3438
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
10765
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
10555
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...
1
10300
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
10069
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
7607
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
5503
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...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
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
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.