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 8 years ago.
Improve this question
Porting some code from my game engine from Mac to Windows in C++, and I get this runtime error: "Vector erase outside range". It works on Mac!
void Entity::runDeferreds() {
for (auto it = deferreds.begin(); it != deferreds.end(); /* nothing */ ) {
if (it->Condition()) {
it->Execution();
it = deferreds.erase(it);
} else {
++it;
}
}
}
This iterates thru a list of "deferred" tasks, which are stored in a std::vector<DeferredCall> called deferreds. If the DeferredCall's Condition() is fulfilled, then its Execution() is run, and it should be removed from the vector. However, instead, I get the aforementioned error!
DeferredCall looks like this, not that it's too important:
struct DeferredCall {
std::function<bool()> Condition;
std::function<void()> Execution;
};
Help?!
EDIT:- Alternative Method
I also tried this, again working on Mac:
deferreds.erase(std::remove_if(deferreds.begin(), deferreds.end(),
[](DeferredCall &call) {
if (call.Condition()) {
call.Execution();
return true;
}
return false;
}
), deferreds.end());
However, in this case I get "vector iterators incompatible".
Although it doesn't answer where your error comes from, you may try to rework the code as follows:
const auto pred = [](Deferred& d){ return !d.Condition(); };
auto itMatch = std::partition( defs.begin(), defs.end(), pred);
const auto action = [](Deferred& d){ d.Execution(); };
std::for_each(itMatch, defs.end(), action);
defs.erase(itMatch, defs.end());
Also, std::vector::erase is guaranteed to return perfectly valid iterator. Could be vector::end() though.
Links: std::partition, std::for_each, std::vector::erase
Related
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 want to build a priority queue of an object having three elements having my own comparator. I tried the following code but is showing error .
#include<bits/stdc++.h>
using namespace std;
class triplet {
public:
int data;
int arr;
int index;
};
bool compare( triplet x, triplet y ){
if(x.data < y.data){
return true;
}
else{
return false;
}
}
int main(){
priority_queue<triplet,vector<triplet>,compare> pq1;
}
getting the following error
enter image description here
Comparison routines must return false if the two elements are equal, but your version returns true.
Try this instead
bool compare(triplet x, triplet y) {
if (x.data < y.data) {
return true;
}
else {
return false;
}
}
or to make it a little simpler like this
bool compare(triplet x, triplet y) {
return x.data < y.data;
}
But the important point is < not <=.
EDIT
Your code also uses compare incorrectly. compare is not a type, so it's not a valid argument for the priority_queue template.
The type of compare is bool (*)(triplet, triplet) so the right way to do this is to use that type in the template and pass the actual comparison function to the constructor. Like this
priority_queue<triplet, vector<triplet>, bool (*)(triplet, triplet)> pq1(compare);
BTW normally you would do this by creating a comparison functor, not a function. It's a little cleaner that way (and potentially more efficient).
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 am just working on a program to find topological order if possible But when I am executing the below program I am getting error
as no matching function call,I am not so good at oops concept , I mentioned in findOrder function
a comment where I am getting the error
I was solving this problem course schedule
class Solution {
public:
vector<int> findOrder(int n, vector<vector<int>>& pa) {
vector<int> g[n];
for(auto p : pa){
int u = p[1],v = p[0];
g[u].push_back(v);
}
vector<bool>visited(n,false);
vector<bool>instack(n,false);
vector<int>order;
for(int i=0;i<n;i++){
if(!visited[i]){
if(!dfs(i,visited,instack,g,order))return {};//getting error here
}
}
reverse(order.begin(),order.end());
return order;
}
bool dfs(int x,vector<int> &visited,vector<int> &instack,vector<int> g[],vector<int>order){
instack[x]=true;
visited[x]=true;
for(int i=0;i<g[x].size();i++){
if(instack[g[x][i]]==true)return false;
else if(!visited[g[x][i]] && !dfs(g[x][i],visited,instack,g))return false;
}
instack[x]=false;
order.push_back(x);
return true;
}
};
In the protoype of your function dfs you have defined the parameters instack and visited as vector<int>, but you are providing variables of type vector<bool> in the calling function.
The easiest way is probably to change:
bool dfs(int x,vector<int> &visited,vector<int> &instack,vector<int> g[],vector<int>order)
into
bool dfs(int x,vector<bool> &visited,vector<bool> &instack,vector<int> g[],vector<int>order)
This is happening because vector is a templated class and the implicit conversion of vector<int> to vector<bool> isn't defined.
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 4 years ago.
Improve this question
I have a view class with private vector of subviews
class MyView {
private:
const std::vector<MySubView> subViews_;
}
In the subview I have these methods:
class MySubView {
public:
const Id getId() const {
return id_;
}
const TimePoint getStartTime() const {
return startTime_;
}
private:
const Id id_;
const TimePoint startTime_;
}
Now in the one of the methods that constructor of MyView calls, I'm generating this array of subviews and set it, which works fine. The problem comes when I try to sort it before assigning.
static std::vector<MySubViews> buildLimitViews(
const ViewData& data);
When I try to sort the vector after building it in this method. I see errors. Sorting is the only part here that does not work.
std::vector<MySubView> buildLimitViews(
const ViewData& data) {
std::vector<MySubView> subViews;
//create views from data and push them in this vector, works fine.
// this sort block below does not work, causes errors.
std::sort(
subViews.begin(),
subViews.end(),
[](const MySubView& lhs,
const MySubView& rhs) {
if (lhs.getStartTime() == rhs.getStartTime()) {
return lhs.getId() < rhs.getId();
}
return lhs.getStartTime() < rhs.getStartTime();
});
return subViews;
}
If I comment out the sort block it works fine, otherwise I see this error:
error: use of deleted function 'MySubView&
MySubView::operator=(MySubView&&)'
Having const non-static member variables breaks assignment, because you cannot modify them. Make subViews_ non-const and it should work.
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 4 years ago.
Improve this question
Since I've started to using std::map I've started to receive assertion error whenever I click on the button. I want to clear all the contents inside my_map and start adding again everytime I click the button.
my.cpp
typedef std::map<str, MyClass> mapper;
mapper my_map;
void add_map(const str& name, MyClass* l) { /...../ }
void invoke_me()
{
Here I initialized variable i with the size of my_map, however, it returns the previous size before it was cleared
int i = my_map.size(); // i = 4, my_map.size() = 1
if (!my_map.empty())
{
for (const auto& it : map)
{
// Assert error: map/set iterator not incrementable
const MyClass& l = it.second;
l.on_clicked();
}
}
}
// cont'd
This should do the neat clearing of my_map, but I don't think it's doing the right job
void clear_map()
{
my_map.clear();
}
implementor.h
struct MyClass
{
std::function<void()> on_clicked;
};
implementor.cpp
MyClass button;
button.on_clicked = [&] {
clear_map();
add_map("MyButton", &button);
};
Well actually on_click is a callback, and it has to be called through the invoke_me function
main.cpp
while (RUNNING)
invoke_me();
I would say that it is because of incorrect map size that's why the it keeps looping even if it's out of bounds, or maybe I'd corrupted or messed up that map.
There might be more issues, but this looks to be the main one:
button.on_clicked = [&] {
clear_map();
add_map("MyButton", &button);
};
You're clearing the map, and this function gets called while you're iterating:
for (const auto& it : my_map)
{
// Assert error: map/set iterator not incrementable
const MyClass& l = it.second;
l.on_clicked();
}
Your iterators are being invalidated by the clear.
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 7 years ago.
Improve this question
I'm using this recursive function to find the tags in a HTML, the output is okay as of now but I'm more interested in the return value of this function find_tag which comes out to be :
-somelargenumber
html
head
body
where the -somelargenumber is the returned value of function which is different every time I run the compiled program.
But according to I made the function it must return 0 at some point in time and not any other value. Can anybody explain this?
#include<string>
#include<regex>
#include<iostream>
#include<iomanip>
using namespace std;
int find_tag(const string& s,vector<string> & cont )
{
smatch m;
if(s.size()>0)
{
if(regex_search(s,m,regex("<(.*)>(.*)</\\1>")))
{
cont.push_back(m[1]);
find_tag(m[2],cont);
find_tag(m.suffix(),cont);
}
else
{
return 0;
}
}
else
{
return 0;
}
}
int main()
{
cmatch m;
vector<string> cont;
string data {"<html><head>head data</head><body>body data</body></html>"};
cout<<find_tag(data,cont);
if(!(cont.empty()))
{
for(auto &a:cont)
{
cout<<a<<endl;
}
}
else
{
cout<<"\n not found";
}
return 0;
}
PS: I am not trying to parse any HTML here. I am just interested in the returned value of the function.
The answer by #hades2510 should resolve your problem.
Suggestions for improvement.
Change the name of the function to find_tags since you are expecting to find many tags in a recursive manner.
You don't need the return type to be int. It doesn't seem useful at all. If you have any tags, they will be returned via the output argument.
If you go by that, the function can be simplified to:
void find_tags(const string& s,vector<string> & cont )
{
smatch m;
if(regex_search(s,m,regex("<(.*)>(.*)</\\1>")))
{
cont.push_back(m[1]);
find_tags(m[2],cont);
find_tags(m.suffix(),cont);
}
}
You need to:
return find_tag(m.suffix(),cont);
Because you don't do that even though the recursion finishes you don't catch it.
The large number you're seeing is whatever the compiler finds on the stack after the recursion finishes. This depends on the compiler but most likely is a combination of the address of find_tag and some from data.