473,395 Members | 1,584 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,395 software developers and data experts.

Vector Elements

Hi people, was curious if there was a way to change all the elements
in a vector without having to loop through and access every one and
then change them.

for example. I have a vector that is 10 000 elements, and one value
that I want to copy them all to. Can I do it without looping?

Any advice is appreciated.
Jul 22 '05 #1
3 1444

"Brett Irving" <br********@gmail.com> wrote in message
news:fc**************************@posting.google.c om...
Hi people, was curious if there was a way to change all the elements
in a vector without having to loop through and access every one and
then change them.

for example. I have a vector that is 10 000 elements, and one value
that I want to copy them all to. Can I do it without looping?

Any advice is appreciated.


Of course

vec.assign(vec.size( ), some_value);

Suggest you familiarise yourself with some standard library documentation.
This site is good, http://www.dinkumware.com/refxcpp.html

john
Jul 22 '05 #2
Brett Irving wrote:
Hi people, was curious if there was a way to change all the elements
in a vector without having to loop through and access every one and
then change them.

erase them all and the resize them with the new value as fill. There
is an oft-forgotten defaulted argument to the vector constructor and
resize function that specifies what the

vector<int> foo(10000); // 10000 elements of zero.
foo.clear();
foo.resize(10000, 5); // 10000 elements of 5;

Of course, the other way is to use fill
fill(foo.begin(), foo.end(), 5);
Jul 22 '05 #3

"Ron Natalie" <ro*@sensor.com> wrote in message
news:41***********************@news.newshosting.co m...
Brett Irving wrote:
Hi people, was curious if there was a way to change all the elements
in a vector without having to loop through and access every one and
then change them.

erase them all and the resize them with the new value as fill. There
is an oft-forgotten defaulted argument to the vector constructor and
resize function that specifies what the

vector<int> foo(10000); // 10000 elements of zero.
foo.clear();
foo.resize(10000, 5); // 10000 elements of 5;

Of course, the other way is to use fill
fill(foo.begin(), foo.end(), 5);


I think using fill is much better approach in this case, from performance
point of view. I don't know if resizing implies reallocation of memory even
for new size smaller than or equal to the old one.

br,
Catalin
Jul 22 '05 #4

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

Similar topics

34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
8
by: cayblood | last post by:
Hello, I have been interested in something kind of like the next_permutation from the STL algorithm library, except that I want it to find possible combinations of vector elements. Here is a more...
4
by: Ole Nielsby | last post by:
I need to build a small array of pointers to a type MyType. The array size can be from 1 upwards, is not known beforehand, typically < 10 but I don't want a small fixed limit. Elements will be...
16
by: kaferro | last post by:
What is the typical way to loop through a vector while deleting certain elements during the loop process? The code below works, but I am wondering if there is a better solution. ...
3
by: peter.vanna | last post by:
Hi, I have the following vector container. *************** class A { public: bool test; }
1
by: Alan | last post by:
I am wondering if anyone has any better idea of how to approach this problem than I do. . . . I have a vector of items (data). I have to do a pairwise comparison of each item to each other item...
19
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 4- Arrays & Pointers, exercise 4.28 * STATEMENT * write a programme to read the standard input and build a vector of integers from values that are read....
3
by: n.torrey.pines | last post by:
I'd like to be able to view two contiguous elements of a vector as a pair. Assuming I'm not accessing the last element, of course, and the element type is not bool, when is it safe to do so,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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...
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...
0
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...
0
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...

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.