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?
Related
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
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()
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];
//...
}
This question already has answers here:
Sorting a std::vector<std::pair<std::string,bool>> by the string?
(5 answers)
Closed 9 years ago.
I have a question about sorting a vector of pairs:
std::vector<std::pair<double,Processor*>> baryProc;
this vector is already filled up with the pairs.
Now I wanted to sort the pairs inside the vector based on the double value inside the pair
EXAMPLE:
suppose I have 3 pairs inside the vector. pair1 is at front and pair 3 is at end. pair2 is in the middle:
pair1(1, proc1)
pair2(3, proc2)
pair3(2.5, proc3)
now i want to sort the pairs based on the double value. So that the order inside the vector is:
pair1(1, proc1)
pair3(2.5, proc3)
pair2(3, proc2)
How could I do this? I am quite stuck.
#include <algorithm>
int main(){
std::vector<std::pair<double,Processor*>> baryProc;
std::sort(baryProc.begin(),baryProc.end());
}
Note that you do not need a custom comparator because the default comparator of pair does the thing you want. It first compares by the first element and if they are identical, it compares the second element in the pair.
In C++, you can have custom comparator functions that specify how to decide whether one element goes before another when sorting. In your case, given 2 pairs, you want the one with the lower value for the first element to go before the other one. You can write a comparator function like so:
// This function returns true if the first pair is "less"
// than the second one according to some metric
// In this case, we say the first pair is "less" if the first element of the first pair
// is less than the first element of the second pair
bool pairCompare(const std::pair<double, Processor*>& firstElem, const std::pair<double, Processor*>& secondElem) {
return firstElem.first < secondElem.first;
}
Now, pass this function into your sort method:
//The sort function will use your custom comparator function
std::sort(baryProc.begin(), baryProc.end(), pairCompare);
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to get a certain element in a list, given the position?
so in python you can get an element in a list in a random access fashion....
list = [1,12,3]
print(list[1])
and it prints 12....
can you do the same thing with c++ list?
I'm talking about this: http://www.cplusplus.com/reference/stl/list/list/
In C++, the nearest along to what you want would be a vector:
std::vector<int> v;
v.push_back(1);
v.push_back(12);
v.push_back(3);
std::cout << v[1] << std::endl; // prints 12
You can use the iterators provided to traverse the vector, too. But once you modify the vector (insert or erase), it becomes invalid.
The actual List class provided (which is a doubly-linked list), doesn't provide this sort of feature.
no. if you use std::list you have to iterate through the list to find a specific element, because list is a double-linked list, elements cannot be accessed with random access operator.
and that's because with lists, it's fast and efficient to insert or delete at any point in the list, thus what was the first element at the beginning could be the third element after modifying the list.
If I understand your question right, you're asking about arrays
int list[3(size)] = {1,12,3};
cout << list[1];
If you are talking about a C++ STL list, no, that is the primary problem with the list.