Finding the min value of a vector of int [duplicate] - c++

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

Related

How does subtracting an array from a pointer work in C++? [duplicate]

This question already has answers here:
What is array to pointer decay?
(11 answers)
Closed last year.
I'm learning about the lower_bound function in C++. To give an example, the author gives the following piece of code:
auto k = lower_bound(array,array+n,x)-array;
if (k < n && array[k] == x) {
// x found at index k
}
I understand that the type of k will be a pointer that holds the address of either the found value or the element after the last element in the array. I, however, don't fully understand the purpose of subtracting the array from the value we get from the lower_bound function. If someone could explain the purpose behind this, I'd really appreciate it. Thanks in advance.
lower_bund will return us a pointer to the element that is not bigger than passed value in the array. Of course we should remember that lower_bound works as we expect only with sorted arrays. Since an array is a sequential set of elements, we can find out the displacement of one element relative to another by subtraction. In other words, subtracting array from lower_bound(), we find the index of the element returned to us by lower_bound()

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];
//...
}

Can pop_back be used to remove certain values from a vector? [duplicate]

This question already has answers here:
C++ Erase vector element by value rather than by position? [duplicate]
(4 answers)
Closed 6 years ago.
I have simple question. I have a vector:
vector<int> SomeVector;
which has some elements inside, say:
{-1, -1, -1, -1, 3, 8, 255}
Is there a way to remove all elements with value -1 from this vector using pop_back? Or if there is any other way that would also be welcomed BUT:
I may not know the index of the elements with value -1
I may not know how many -1's there are in the vector
Just a new student here, any help would be great, thanks in advance...
I would use the erase-remove idiom for such a task
// Remove all elements with value -1 from the vector
vec.erase(std::remove(vec.begin(), vec.end(), -1), vec.end());
Using just pop_back in combination with something like back() would treat the vector as a stack and pop off as many -1 as there are at its end.

Why i am not able to find index of an element in the vector? [duplicate]

This question already has answers here:
Finding the position of the maximum element
(6 answers)
Closed 7 years ago.
I am getting the max element right but not index of maximum element.index of element is coming out to be equal to max element.
long int maxi=*max_element(segments.begin(),segments.end());
long int index=*find(segments.begin(),segments.end(),maxi);
Try this:
long int index=find(segments.begin(),segments.end(),maxi) - segments.begin();
Find returns an iterator to the maximum element, which for vectors behaves mostly like a pointer. What you were doing was dereferencing it (which just gets you what it points to, the maximum element). Instead, compute the distance between that iterator and the start iterator to get the index.

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?