Remove from the beginning of std::vector [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a vector of the following data structure
struct Rule {
int m_id = -1;
std::wstring name;
double angle;
};
std::vector<Rule>& topPriorityRules;
and I am erasing the first element of the vector using
topPriorityRules.erase(topPriorityRules.begin());
Is there any other good alternative for removing elements from the front of a std::vecor?

Given
std::vector<Rule>& topPriorityRules;
The correct way to remove the first element of the referenced vector is
topPriorityRules.erase(topPriorityRules.begin());
which is exactly what you suggested.
Looks like i need to do iterator overloading.
There is no need to overload an iterator in order to erase first element of std::vector.
P.S. Vector (dynamic array) is probably a wrong choice of data structure if you intend to erase from the front.

Three suggestions:
Use std::deque instead of std::vector for better performance in your specific case and use the method std::deque::pop_front().
Rethink (I mean: delete) the & in std::vector<ScanRule>& topPriorityRules;
Use std::vector::erase() (see Caleth's comment below).

Related

How do I get the pointer to the first item in a vector for qsort's first argument? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I know that to get the pointer to the first element of a vector, you can do:
&myvector[0];
//or
&myvector.front();
I want to use the pointer above to insert it as the first argument for qsort. The problem is, when I do this:
qsort(graph->&edge[0], ..., ..., myComparatorFunction);
I get this error expected unqualified-id before '&' token. The graph refers to an instance of a self-made class below:
class Graph {
public:
int V, E;
vector<Edge> edge;
};
The instance is created using:
Graph* createGraph(vector<Edge> edge) {
Graph* graph = new Graph;
graph->edge = edge;
return graph;
}
How do I get rid of the error while achieving what I want?
Your syntax is wrong.
Compose the expressions like so:
The vector is graph->edge.
Its first element is graph->edge[0].
The address of that is &graph->edge[0].
(This could also be spelt graph->edge.data(), using vector's own interface more directly.)
Your graph->&edge[0] is just not meaningful.
Also I recommend std::sort over qsort unless you know you need the latter for some reason. It's type-safe and it can take more "things" (e.g. capturing lambdas, and other inlineable functors).
And, it will try to be the best sort it can be. The same is true of qsort (despite the name, which suggests it'd always use quicksort), but std::sort's worst-case algorithmic complexity is required to be better than qsort's is permitted to be.

Hi, I am trying to add specific nodes of a set to a list, the list being in a hash table [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have the following set and list declarations:
set<mystruct> my_entries;
unordered_map<string, list<_Rb_tree_const_iterator<mystruct>>> mylist;
What I am trying to do is basically insert some specific nodes of the set to the list as well after inserting it into the set.
auto inserted = my_entries.insert(entry);
mylist[name].push_back(*inserted.first);
I get the following error:
error: no matching function for call to
'std::list<std::_Rb_tree_const_iterator<mystruct> >::push_back(const
mystruct&)’
mylist[name].push_back(*inserted.first);
note: no known conversion for argument 1 from 'const mylist’ to
'std::list<std::_Rb_tree_const_iterator<mylist> >::value_type&& {aka
std::_Rb_tree_const_iterator<mylist>&&}'
Any ideas whats gone wrong?
Thanks.
As the comments describe, you should not be attempting to use internal types from the standard library. You can store the iterator as follows:
set<mystruct> my_entries;
unordered_map<string, list<set<mystruct>::const_iterator>> mylist;
// ...
mylist[name].push_back(inserted.first);
Whenever storing iterators, it's important to understand the rules governing when such iterators can become invalid. In the case of std::set::insert you are guaranteed:
No iterators or references are invalidated.

Printing a Vector that was passed to a print function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
This is the code that I have written
I am getting an error when trying to print out the vector values and I can not figure out why.
I passed the array to the function print(&v1).
Can someone help me figure out why this error is occurring and if there is a better alternative way to print out the vector elements in my function.
You correctly used -> in print1 instead of . (like in print) to operate on the pointer to the vector. This works because x->y is equivalent to (*x).y, meaning you correctly dereferenced the pointer to the vector before accessing it.
Except for []. Here you also have to dereference the pointer before using []. So:
cout << (*v)[x] << endl;
There is no abbreviation (also called "syntactic sugar") for (*x)[] like there is for (*x).y, so you must do it manually.
The error message is confusing because using [] on a pointer is valid - x[y] is equivalent to *(x+y), which means you are doing pointer arithmetics: You use v as if it was a (C-style) array of vectors, and you try to get the xth element from this array of vectors. Lucky for you, the compiler doesn't know how to << a Vector with cout - but if it could, the code would compile and do something you (probably) did not intend.

C++ function that compares runtime of different sort algorithms [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hello I am doing project where I have different sorting algorithms (QuickSort, BubbleSort etc).
I measure time of each of them and at the end I need to make some comparison which one is the fastest, and so on.
bool testyBubblesort(){
long int before = GetTickCount();
a.BubbleSort_int();
long int after = GetTickCount();
double dif = double((after - before));
result &= testEqual(zoradane.vypis_pola(), a.vypis_pola(),dif, "Test sort array-int");
comparison(dif);
vypisVysledkuTestu(result, "Testy triedenia BubbleSortom");
return result;
}
This is my function, it tests whether arrays are sorted and measures time. The important part is comparison(dif); This should be the function that compares all sorts. Note that i use this function in other sorts too.
void comparison(double time){
vector<int> all_times;
all_times.push_back(time);
}
So i was thinking make vector array and then sort it is good idea but I dont know how I always get some error.
Can u help me ?
Thanks
You aren't comparing to anything because your vector only has one element.
You should replace "comparison" with just an insertion into a vector that is declared globally. Then, at the top-level (wherever calls testyBubblesort), and perform your comparisons there.
A more robust method would be to make a vector, where MyStruct is declared as a double and a string, so that you store {time, "Bubble Sort"} (allowing you to associate the runtime with the sorting algorithm used. Then at the top-level, just use the built-in sort function (you will have to use the version that accepts a function to define how to order MyStruct) on the vector and grab the first object from the vector. Then just print out the string.

Is it possible to remove certain elements from a vector based on the information from another vector? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Lets say we have the vectors A and B which
contain objects not primitive data.
The B vector would contain information about which elements to
be removed from the vector A. I don't want to rely on index if possible.
I suspect that in Java this could be done with a method call
to removeAll where you pass the list of things to be
removed as an argument.
Is this possible with the C++ language?
Thanks!
If the vectors are sorted, you can make use of std::set_difference. In fact of you don't sort them first you can't get better than n^2 in terms of complexity. So my suggestion is: sort the vectors and use set_difference.
Without knowing the specifics of what A and B contain, it is possible to do something like this:
std::vector<int> A;
std::vector<int> B;
// fill in your vectors
A.erase(std::remove_if(A.begin(), A.end(), [&](int i)
{
return std::find(B.begin(), B.end(), i) != B.end();
}), A.end());
This will search B for anything that matches in A. If something is found, it will be removed.
This process can be decomposed into:
1) Choose the comparison method that qualifies an element to be deleted,
2) Find a way to delete the selected elements (either as a continuous range or one by one).
Why should this be a problem at all? You only have to find a way to compare the elements on vector B with the elements on vector A, and based on that, you build a list of the elements to be removed, and then you remove them.
Your vector, of course, could be a vector of some class, which contains all the properties you need to do the comparison between the objects in A and B.