相关疑难解决方法(0)

如何使用反向迭代器调用erase

我想做这样的事情:

for ( std::list< Cursor::Enum >::reverse_iterator i = m_CursorStack.rbegin(); i != m_CursorStack.rend(); ++i )
{
    if ( *i == pCursor )
    {
        m_CursorStack.erase( i );
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是擦除需要迭代器而不是反向迭代器.有没有办法将反向迭代器转换为常规迭代器或从列表中删除此元素的另一种方法?

c++

165
推荐指数
4
解决办法
6万
查看次数

如果在从头到尾迭代时调用map元素上的erase()会发生什么?

在下面的代码中,我遍历一个map并测试是否需要擦除一个元素.擦除元素并继续迭代是否安全,或者我是否需要在另一个容器中收集密钥并执行第二个循环来调用erase()?

map<string, SerialdMsg::SerialFunction_t>::iterator pm_it;
for (pm_it = port_map.begin(); pm_it != port_map.end(); pm_it++)
{
    if (pm_it->second == delete_this_id) {
        port_map.erase(pm_it->first);
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:当然,我读了这个问题,我认为这个问题没有关系,但回答了我的问题.

c++ iterator stl

132
推荐指数
2
解决办法
8万
查看次数

remove_if等效于std :: map

我试图根据特定情况从地图中删除一系列元素.我如何使用STL算法?

最初我想使用remove_if但不可能因为remove_if不适用于关联容器.

是否有适用于地图的"remove_if"等效算法?

作为一个简单的选项,我想到循环遍历地图并擦除.但是循环遍历地图并删除安全选项?(因为迭代器在擦除后变为无效)

我使用以下示例:

bool predicate(const std::pair<int,std::string>& x)
{
    return x.first > 2;
}

int main(void) 
{

    std::map<int, std::string> aMap;

    aMap[2] = "two";
    aMap[3] = "three";
    aMap[4] = "four";
    aMap[5] = "five";
    aMap[6] = "six";

//      does not work, an error
//  std::remove_if(aMap.begin(), aMap.end(), predicate);

    std::map<int, std::string>::iterator iter = aMap.begin();
    std::map<int, std::string>::iterator endIter = aMap.end();

    for(; iter != endIter; ++iter)
    {
            if(Some Condition)
            {
                            // is it safe ?
                aMap.erase(iter++);
            }
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ stl map

113
推荐指数
5
解决办法
5万
查看次数

如何过滤std :: map中的项目?

我大致有以下代码.这可以更好或更有效吗?也许用std::remove_if?您可以在遍历地图时从地图中删除项目吗?我们可以避免使用临时地图吗?

typedef std::map<Action, What> Actions;
static Actions _actions;

bool expired(const Actions::value_type &action)
{
  return <something>;
}

void bar(const Actions::value_type &action)
{
  // do some stuff
}

void foo()
{
  // loop the actions finding expired items
  Actions actions;
  BOOST_FOREACH(Actions::value_type &action, _actions)
  {
    if (expired(action))
      bar(action);
    else
      actions[action.first]=action.second;
    }
  }
  actions.swap(_actions);
}
Run Code Online (Sandbox Code Playgroud)

c++ boost stl

27
推荐指数
3
解决办法
2万
查看次数

在迭代时从地图(或任何其他STL容器)中删除/删除内容

据称,当迭代器变为无效时,您不能在迭代时擦除/删除容器中的元素.删除满足特定条件的元素的(安全)方法是什么?请只是stl,没有提升或tr1.

编辑 如果我想删除符合某个标准的元素,可能使用仿函数和for_each或擦除算法,是否有更优雅的方法?

c++ stl

26
推荐指数
3
解决办法
2万
查看次数

我是否应该看到`for`循环中的计数器在其体内发生了变化?

我正在读别人的代码,他们分别在循环内增加for循环计数器,以及包括通常的事后想法.例如:

for( int y = 4; y < 12; y++ ) {
    // blah
    if( var < othervar ) {
        y++;
    }
    // blah
}
Run Code Online (Sandbox Code Playgroud)

根据其他人编写和阅读的大部分代码,我应该期待看到这个吗?

language-agnostic for-loop loop-counter

16
推荐指数
4
解决办法
1656
查看次数

从基于范围内的地图中擦除是否安全?

我经常使用这种语法来循环一个std::map

for( const auto& my_pair: my_map )
Run Code Online (Sandbox Code Playgroud)

我可以my_map.erase( my_pair.first );安全地打电话吗?

c++ foreach map range

5
推荐指数
1
解决办法
2304
查看次数

使用remove_if作为地图容器

我试图将remove_if模板用于地图容器,但我收到模板参数的编译器错误。我不明白为什么。

int main()
{
  map<const int, int> intmap;

  intmap[1] = 1;
  intmap[2] = 2;
  intmap[3] = 3;
  intmap[4] = 4;

  auto isOdd = [&](pair<const int, int> it)->bool 
     { return static_cast<bool>(it.second % 2); };

  isOdd(*(intmap.begin()));

 remove_if(intmap.begin(), intmap.end(), isOdd); 
}
Run Code Online (Sandbox Code Playgroud)

此remove_if引发编译器错误。有什么建议可以解决吗?

错误消息是

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\utility(260) : error C2166: l-value specifies const object
        C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\utility(259) : while compiling class template member function 
        'std::pair<_Ty1,_Ty2> &std::pair<_Ty1,_Ty2>::operator =(std::pair<_Ty1,_Ty2> &&)'
        with
        [
            _Ty1=const int,
            _Ty2=int
        ]
        maperaseif.cpp(29) : see reference …
Run Code Online (Sandbox Code Playgroud)

c++ stl remove-if

4
推荐指数
1
解决办法
2315
查看次数

地图上的C++循环没有检测到地图结束的变化

我在循环通过地图时遇到问题(std :: map).

在我的循环中,有一个函数调用,有时(并不总是)擦除同一个映射的元素.使用此函数后,有一些代码使用这些映射信息作为输入.

在此功能擦除任何元素后,我没有任何问题,除了在地图的最后一个元素被删除的独特情况.

我的循环semms不明白地图的最后一个元素与它开始运行时的不一样,并且会尝试对不存在的元素进行操作,从而造成崩溃.

在我看来,对循环描述的myMap.end()调用无法使用地图的新end()更新自身.

代码的相关部分如下:

for(std::map<int, ConnectionInfo>::iterator kv = myMap.begin(); kv != myMap.end(); ++kv) {
        int thisConnectionID=kv->first; //This is where I get garbage when the loop enters when it shouldnt;
        ConnectionInfo currentConnectionInfo=kv->second; //This is where I get garbage when the loop enters when it shouldnt;
        status=eraseSomeMapElementsIfNecessary(thisConnectionID,currentConnectionInfo.DownPacket); //this function might erase elements on myMap. This generates no problems afterwards, except when the end element of myMap is erased
        ... //Next parts of the code …
Run Code Online (Sandbox Code Playgroud)

c++ containers dictionary iterator loops

4
推荐指数
1
解决办法
140
查看次数

C++:从地图中删除迭代器,然后递增到下一个迭代器

此方法导致中止错误:"map/set iterator not incrementable." 由于在if失败之后并且确定应该擦除的虚拟迭代器(并且是),继续到映射中的下一个迭代器++_iter失败,因为_iter它不再是有效的对象/指针.

迭代地图的正确程序是什么,并且能够在整个过程中删除单个项目?

typedef std::map<std::string, BITMAP*> MapStrBmp;
typedef MapStrBmp::iterator MapStrBmpIter;
\\...
void BitmapCache::CleanCache() {
    //Clean the cache of any NULL bitmaps that were deleted by caller.
    for(MapStrBmpIter _iter = _cache.begin(); _iter != _cache.end(); ++_iter) {
        if(_iter->second != NULL) {
            if((_iter->second->w < 0 && _iter->second->h < 0) == false) continue;
        }
        _cache.erase(_iter);
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ iterator stdmap allegro

2
推荐指数
1
解决办法
3003
查看次数

C++ - begin() 返回带有非空列表的 end() 迭代器

正如问题所暗示的那样,我对迭代器和列表有一种非常奇怪的行为。因此,(类)问题需要一个函数来擦除列表中满足条件的所有元素,并且当我试图涵盖我有一个所有元素都相同的列表的情况时,我发现最后一个元素仍然存在.

这是代码:

void esborra_tots(list<Estudiant>& t, int x) {
    list<Estudiant>::iterator it;
    list<Estudiant>::iterator itend = t.end();

    for (it = t.begin(); it != t.end(); it++) {
        if ((*it).consultar_DNI() == x) {

            t.erase(it);
            if (t.empty()) return;
            else it = t.begin();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用itend只是为了在调试时查看值。这是会议: 看到列表 *t* 不为空,但 t.begin() 返回与 t.end() 相同

这怎么可能?PD:我不是在寻找解决这个问题的其他方法。

c++ c++11 visual-studio-code

1
推荐指数
1
解决办法
130
查看次数

在列表C++上执行递归

我试图确定我可以使用标准列表从列表中删除的最大项目数,以获得最小大小.但是,它最终会导致内存访问不良.

这是我的递归函数:

int step (list<int> mylist) {
    int count = mylist.size();
    // Terminations
    if (!checkRemaining(mylist)) {
        return mylist.size();
    }
    if (mylist.empty()) {
        return 0;
    }
    //printf("mysize: %d\n", mylist.size());

    // Else we do not terminate first
    for (auto i=mylist.begin(); i != prev(mylist.end()); ++i)
    {
        if ((*i + *next(i))%2 == 0) // Problem starts from here, bad access
        {
            mylist.erase(next(i));
            mylist.erase(i);
            printf("this size %lu\n", mylist.size());

            list<int> tempList = mylist;
            for (auto it = tempList.begin(); it != tempList.end(); it++) {
                printf("%d ", *it); …
Run Code Online (Sandbox Code Playgroud)

c++ recursion linked-list

0
推荐指数
1
解决办法
124
查看次数

C++ 地图容器erase() 分段错误

我使用此代码从地图容器中删除等于某个 int 的元素。

for(auto x:m){
  if((x.second)==element)m.erase(x.first);
}
Run Code Online (Sandbox Code Playgroud)

结果是分段错误。我也试过这个:

for(map<int,int>::iterator i=m.begin();i!=m.end();i++){
  if((i->second)==element)m.erase(i);
}
Run Code Online (Sandbox Code Playgroud)

结果一样。如果将 i++ 放入 if/else 程序将冻结/循环或其他什么。我怎样才能解决这个问题?

c++ stdmap

0
推荐指数
1
解决办法
225
查看次数