Can range based for loop work for assignment? [duplicate] - c++

This question already has answers here:
Setting vector elements in range-based for loop [duplicate]
(4 answers)
Closed 6 years ago.
Is it possible to use/implement a ranged base loop to assign numbers to an array?
What I want:
for (auto i : X){
i = 1;
} //I want this to fill the array with 1.

Is it possible to use/implement a ranged base loop to assign numbers to an array?
Yes, use a reference:
for (auto & i : X){
// ^
i = 1;
}

Related

Finding the min value of a vector of int [duplicate]

This question already has answers here:
How to find minimum value from vector?
(7 answers)
Closed 4 months ago.
I have a vector of integers something like this:
vector<int>distances = {1,5,7,15};
And i would like to store the minimal value of those in a int variable like this:
int varname = minValueOfDistances;
Is there any function in c++ that does that? Or do I have to create one?
Yes! there is a builtin Function
vector<int> distances = {1, 5, 7, 15};
cout << *min_element(distances.begin(), distances.end());
what does min_element needs? and what does it return?
it needs a pointer of the beginning Range you wants to get the minimum element and the end of your range
and what does it return?
It returns a pointer where is this element exactly .. but we want the value of this element .. so we use * to get the value of it

Is it possible to iterate through pair of values using for loop in c++? [duplicate]

This question already has answers here:
What are use cases for structured bindings?
(4 answers)
Closed 2 years ago.
Is something like this is possible or there is any way to assign the pair values separately in for loop?
vector<pair<int, int>> arr;
// Input some values here in arr
for(auto &[x,y]: arr){
cout<<x<<" "<<y;
}
Your code is actually valid code in C++17 or later, as #MikeCAT said. You're using what's called a structured binding. It can unpack values from arrays or simple class types, in this case from pairs.

What does the expression for(int& item : v4) means : [duplicate]

This question already has answers here:
C++11 range based loop: How does it really work
(3 answers)
C++ ranged based for loop
(2 answers)
'colon' and 'auto' in for loop c++? need some help understanding the syntax
(3 answers)
Closed 6 months ago.
v4 is a vector, the loop is to iterate through the vector list and display the elements in the vector. what does the expression inside the for loop means.
the code is as follows :
This is a range-based for loop. It iterates over all the elements of the v4 vector, and lets you process via the item variable in each iteration.
It iterates through the container v4, whatever that is, let's say it's a vector
The & in "int&" means that it's a reference, if you change the item, it's gonna change the value in the as well.
Similar to
for (int i = 0; i < v4.size(); i++){
item = v4[i];
//...
}

How to make a range to use range based for (to replace traditional for)? [duplicate]

This question already has answers here:
C++0x way to replace for(int i;;) range loops with range-based for-loop
(1 answer)
Is there a range class in C++11 for use with range based for loops?
(8 answers)
Closed 4 years ago.
I have the following loop in more then one place:
for(int step = 1; step < timesteps; step++) ...
Then I had to change timesteps to start at 0. And I have to change it in more then one place.
May I take advantage of range based for loops in this situation? So may be I can write just
for(auto &step : timesteps) ...
In that case what kind of timesteps should be used? std::vector timesteps?
Before I forget to mention, timesteps is just an int i.e. int timesteps = 10;. Unknown at compile time, the user will define it.
p.s.: stl is preferred over boost.

Vector of Sets in C++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to do the vector of sets in C++?
I want to have a set for the different levels that are in my code. A set at each level will be holding integer values. The number of these sets should be dynamic depending on the number of levels required ( which is given as input ).
For this, I wanted to have a dynamic set structure. How can I achieve this? Can I go for a vector with as many pointers to the sets as required? How do I achieve this? Is there any other method.
Can somebody give me a snippet for it?
vector<set<int> > my_sets;
Adding an element to i-th set:
int number;
//...
my_sets[i].insert(number);
Searching an element in i-th set:
if(my_sets[i].find(number) != my_sets[i].end())
{
// Number found
}
Iterate over i-th set:
for(set<int> :: iterator it = my_sets[i].begin(); it != my_sets[i].end();++it)
{
// do something with integer value *it
}
Add a new set:
set<int> temp;
temp.insert(a);temp.insert(b);//...
my_sets.push_back(temp);
Erase i-th set:
my_sets.erase(my_sets.begin() + i );
A vector of sets is simply std::vector<std::set<type>>. Are you looking for something else?