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. 3 1457
"Brett Irving" <br********@gma il.com> wrote in message
news:fc******** *************** ***@posting.goo gle.com... 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
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(1000 0, 5); // 10000 elements of 5;
Of course, the other way is to use fill
fill(foo.begin( ), foo.end(), 5);
"Ron Natalie" <ro*@sensor.com > wrote in message
news:41******** *************** @news.newshosti ng.com... 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(1000 0, 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 "push_back" a copy of this into a
vector V. This is repeated many times in an iterative process.
Ok whenever I "push_back" a copy of Class A, I also want to assign a
pointer contained in an exisiting instance of a Class B to this
|
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
detailed example of what I want:
Given a vector containing an arbitrary number of vectors, each of which
contains an arbitrary number of elements, generate a new vector in
which each element consists of one element taken from its corresponding...
|
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 sorted by pointer address
except the first one which is special.
I can declare
std::vector<MyType*builder;
|
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.
vector<intvTmp;
vTmp.push_back(1);
vTmp.push_back(2);
vTmp.push_back(1);
vTmp.push_back(2);
|
by: peter.vanna |
last post by:
Hi,
I have the following vector container.
***************
class A
{
public:
bool test;
}
| |
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 and apply logic to see if
one should be deleted. In the past I have done this using for
statements, like:
for (int i = 0; i < input_vector.size(); i++)
for (int j = i; i < input_vector.size(); j++)
|
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. allocate an array of the same
size as the vector and copy elements from the vector into the array
*/
|
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, from the language
definition point of view?
const pr& p = *(const pr*)(&v);
// pr - either std::pair or hand-defined pair of elements
|
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,...
|
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: 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();...
|
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: 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
| |