标签: stdmap

VS2010 RC - 调试器中只有100个std :: map元素

当我想要查看std :: map容器的所有元素时,我在VS 2010 RC中调试我的应用程序时遇到一个小问题.

当调试器到达断点并且我想在元素检查器中检查映射的值(在'Locals'窗口中以及在用鼠标悬停变量名后的弹出窗口中)并且我向下滚动列表它停在100元素上的元素,我不能下一个元素.地图包含200多个元素(地图的计数器参数正确显示)但我无法在元素检查器中查看它们.

即使在最简单的std::map<int, int>200个int值中也会出现问题.

也许这是一个设置问题,但我尝试了很多组合,但它仍然无法正常工作.也许有人有一些想法如何解决这个问题.

预先感谢您的帮助.

c++ debugging stdmap visual-studio-2010 visual-studio

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

如何将不可复制和不可移动的对象分配到std :: map中?

我有一个对象,我想限制只在一个内部分配std::map.这是简化的代码:

#include <map>

class Value
{
public:
    Value(int value) { _value = value;}
    Value(const Value&) = delete;
    Value& operator=(const Value&) = delete;
    Value(Value&&) = default;     // ***
    void* operator new(size_t) = delete;    // not on free store
private:
    int _value;
};

class Container
{    
public:
    Container();
    Value* addToMap(int key) {
        auto ret = _map.emplace(key, key);
        return &(ret.first->second);
    }
private:
    std::map<int, Value> _map;
};
Run Code Online (Sandbox Code Playgroud)

为了使用CLang在Mac上进行编译,我必须添加一个标记为星号的行,请求默认的移动构造函数.但是,在Windows Visual Studio中编译时,此行会导致C2610错误.看起来VS2013 C++ 11不合规包括无法生成默认移动构造函数.我是否有不同的方法在标准映射中分配一个可以跨平台编译的对象,还是我必须实现自己的移动构造函数?

c++ stdmap move-semantics c++11

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

在地图中存储引用

我试图将一个foo对象存储到一个std::reference_wrapper,但我最终得到一个我不明白的编译器错误.

#include <functional>
#include <map>

struct foo
{
};

int main()
{
    std::map< int, std::reference_wrapper< foo > > my_map;
    foo a;
    my_map[ 0 ] = std::ref( a );
}
Run Code Online (Sandbox Code Playgroud)

编译器错误相当冗长,但归结为:

error: no matching function for call to ‘std::reference_wrapper<foo>::reference_wrapper()’
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

c++ reference stdmap

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

为什么我可以将boost map_list_of传递给采用地图但不是构造函数的函数?

我正在尝试std::map通过使用boost传递地图内容来构造一个以参数为参数的对象map_list_of.

这给出了编译错误,但是,当我尝试使用带有a的常规函数​​执行相同操作时std::map,它编译得很好!

#include <map>
#include <boost/assign.hpp>

struct Blah
{
    Blah(std::map<int, int> data) {}
};

void makeBlah(std::map<int, int> data) {}

int main()
{
    Blah b(boost::assign::map_list_of(1, 2)(3, 4));    // Doesn't compile.

    makeBlah(boost::assign::map_list_of(1, 2)(3, 4));  // Compiles fine!
}
Run Code Online (Sandbox Code Playgroud)

我得到的编译错误是:

error: call of overloaded ‘Blah(boost::assign_detail::generic_list<std::pair<int, int> >&)’ is ambiguous
note: candidates are: Blah::Blah(std::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >)
note:                 Blah::Blah(const Blah&)
Run Code Online (Sandbox Code Playgroud)

什么是歧义,为什么它不会影响常规的functoin makeBlah,据我所知,它与Blah构造函数具有相同的签名?

有没有更好的方法来实现这一点,而不是制作一个makeBlah将构造函数作为对象的函数Blah,因为它看起来像我将要做的那样?

(顺便说一下,我在单元测试中这样做,map_list_of用于使测试输入数据创建更具可读性)

c++ constructor boost stl stdmap

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

通过移动分配具有不可复制(但可移动)键的映射的错误

为什么这不起作用:

#include <memory>
#include <map>

std::map<std::unique_ptr<char>, std::unique_ptr<int>> foo();
std::map<std::unique_ptr<char>, std::unique_ptr<int>> barmap;

int main(){
  barmap=foo();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这样做:

#include <memory>
#include <map>

std::map<std::unique_ptr<char>, std::unique_ptr<int>> foo();
std::map<std::unique_ptr<char>, std::unique_ptr<int>> barmap;

int main(){

  std::map<std::unique_ptr<char>, std::unique_ptr<int>> tmp(foo());
  using std::swap;
  swap(barmap, tmp);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这与地图中的键类型不可复制这一事实有关(std :: map是否需要?).编译时的相关错误行g++ -std=c++14:

/usr/include/c++/4.9/ext/new_allocator.h:120:4: error: use of deleted function ‘constexpr std::pair<_T1, _T2>::pair(std::pair<_T1, _T2>&&) [with _T1 = const std::unique_ptr<char>; _T2 = std::unique_ptr<int>]’
  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
    ^
In file included from /usr/include/c++/4.9/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/4.9/memory:62,
                 from pairMove.cpp:1:
/usr/include/c++/4.9/bits/stl_pair.h:128:17: …
Run Code Online (Sandbox Code Playgroud)

c++ stdmap move-semantics c++14

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

从std :: map中删除前N个项目?

尝试编写一个方法,从std :: map中删除第一个(最低键控的)N个项目.试过这个:

void EraseNMapElements(const int numElementsToRemove){

    const int originalSize = _map.size();     
    auto eraseIter = _map.begin();
    std::advance(eraseIter, numElementsToRemove);

    _map.erase(_map.begin(), eraseIter);

    assert(_map.size() == (originalSize - numElementsToRemove)) || (0 == originalSize) || (0 == _map.size()));
}
Run Code Online (Sandbox Code Playgroud)

当元素数量超过请求删除的数量时,它可以工作.因此,如果我有五个元素,请求删除2,最后3个元素仍然存在.但是,如果我有一个元素并请求擦除2,我仍然剩下一个元素.

有没有一个简洁的方法来涵盖这个?我可以推送一个IF语句来检查numElementsToRemove大于map.size()但是必须有一个更好的解决方案吗?

c++ stdmap c++11

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

清除锁定下的std :: map与移动到临时对象

我正在使用std :: map并且其中包含大量元素.如果我需要清除地图,我可以在上面调用clear().它可能需要一些时间来清除,特别是如果在多线程环境中的锁定下完成,它可以阻止其他调用.为了避免调用clear(),我尝试了这个:

std::mutex m;
std::map<int, int> my_map; // the map which I want to clear

void func()
{
    std::map<int, int> temp_map;
    {
        std::lock_guard<std::mutex> l(m);
        temp_map = std::move(my_map);
    }
}
Run Code Online (Sandbox Code Playgroud)

这会将my_map移动到锁定下的temp_map,这会将其清空.然后一旦func结束,temp_map将被销毁.

这是一个更好的方法来防止锁定很长一段时间吗?是否有任何表演命中?

c++ multithreading mutex stdmap c++14

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

How to efficiently move (some) items from one std::map to another?

I have two std::map<> objects a and b and would like to move (extract + insert) some elements (nodes) from one map to the other based on some predicate p.

for (auto i = a.begin(); i != a.end(); ++i)
    if (p(*i))
        b.insert(a.extract(i))
Run Code Online (Sandbox Code Playgroud)

This code segfaults in clang. I assume the problem is the increment of i after its node has been extracted from a.

Is the right/only way to fix this by using a post-increment?, E.g.:

for …
Run Code Online (Sandbox Code Playgroud)

c++ stl stdmap c++17

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

从C++走向C:替代std :: map?

我正在寻找std :: map <long,int>的简约替代品,它将进入Windows内核驱动程序,所以它应该非常快......预计它将保持相对较小(工作集中约200)的量键和大量的插入.

寻找可以降低关键搜索成本的解决方案.

c c++ stdmap wdk

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

如何在std :: map中try_emplace POD结构?

我有一张int -> { basic types }我需要存储的地图.

我想简单地创建一个struct { int f1, int f2; };并直接存储值,在商店中就地构建结构.我不希望有任何重复的键,所以try_emplace看起来很理想.

我写了这段代码:

// mcve.cpp
#include <map>
#include <string>

struct various { int f1, f2; };
using map_t = std::map<int, various>;

void
example()
{
    map_t dict;

    //dict.try_emplace(1, 2);
    dict.try_emplace(1, 1, 2);
    //dict.try_emplace(1, {1, 2});
}
Run Code Online (Sandbox Code Playgroud)

但这些选项都不起作用.

使用clang ++我得到这样的错误.(版本:clang版本5.0.1(标签/ RELEASE_501/final))

/opt/local/libexec/llvm-5.0/include/c++/v1/tuple:1365:7: error: no matching
      constructor for initialization of 'various'
      second(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
      ^      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

### ... many lines ...

mcve.cpp:14:7: note: in instantiation of …
Run Code Online (Sandbox Code Playgroud)

c++ struct stdmap emplace c++17

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