标签: stdmap

我是否需要删除静态std :: map?

在某些类中,我有一个带有指针的静态std :: map.我的问题是,如果我需要在程序结束时删除或自动释放此内存.我关心的是当删除std :: map时,是否通过析构函数正确删除了存储在内部的指针.

谢谢.

c++ stl stdmap

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

map <string,vector <pair <int,int>>>推回对吗?

我有这个map<string, vector <pair<int, int> > >变量,并且正在推回一个值,但是code :: blocks告诉我该对没有名为push_back的成员函数。我应该怎么做才能使它推回而不是pair<>.push_back()

这基本上是我在做什么:

map<string, vector <pair<int, int> > > T;
for(int x = 0; x < data.size(); x++)
     T[data[x].str].push_back(data[x].PAIR)
Run Code Online (Sandbox Code Playgroud)

错误是:

error: no matching function for call to 'std::vector<std::pair<int, int>,
  std::allocator<std::pair<int, int> > >::push_back(std::map<int, int, 
    std::less<int>, std::allocator<std::pair<const int, int> > >&)'
Run Code Online (Sandbox Code Playgroud)

c++ vector stdmap push-back std-pair

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

如何在地图中插入结构?

删除给定行的注释后,我在代码中编译错误.我无法将结构插入到地图中,而插入整数很好.如何修复错误?

# include <iostream>
# include <map>

using namespace std;

struct node
{int test;} temp;

int main()
{
    temp.test = 24;
    int test = 30;
    map<node, bool> mymap1;
    map<int, bool> mymap2;
    //mymap1.insert(make_pair(temp, true));
    mymap2.insert(make_pair(test, true));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ struct dictionary insert stdmap

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

使用std :: map而不是vector <pair <string,string >>>我会看到性能提升吗?

我目前有一些代码,我使用的vectorpair<string,string>.这用于存储来自XML解析的一些数据,因此,该过程在某些地方非常慢.在试图加快整个过程中,我想知道是否有将是从交换任何性能方面的优势vector<pair<string,string> >std::map<string,string>?我可以编写代码并运行一个分析器,但我想我会看到我是否能得到一个答案,表明首先会有一些明显的性能提升.我不需要进行任何排序,我只是将项添加到向量中,然后在稍后阶段迭代内容并进行一些处理 - 我不需要排序或任何这种性质.我猜也许我不会获得任何性能提升,但我从来没有真正使用过std::map,所以我不知道没有要求或编码全部.

c++ stl stdmap stdvector

3
推荐指数
3
解决办法
2759
查看次数

无法插入到std :: map(G ++)

我有以下麻烦:

struct ServerPP {
    std::string name;
    int id;
    int expires;
};
std::map<std::string, std::set<ServerPP>> RemindTable;

int test(std::string email, ServerPP serv)
{
    RemindTable[email].insert(serv); // error when compile in this row below
}
Run Code Online (Sandbox Code Playgroud)

g ++中的错误:

In file included from /usr/include/c++/4.4/string:50,
                 from /usr/include/c++/4.4/bits/locale_classes.h:42,
                 from /usr/include/c++/4.4/bits/ios_base.h:43,
                 from /usr/include/c++/4.4/ios:43,
                 from /usr/include/c++/4.4/istream:40,
                 from /usr/include/c++/4.4/sstream:39,
                 from stdafx.h:19,
                 from ActiveReminder.cpp:4:
/usr/include/c++/4.4/bits/stl_function.h: In member function 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = ServerPP]':
/usr/include/c++/4.4/bits/stl_tree.h:1170:   instantiated from 'std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const …
Run Code Online (Sandbox Code Playgroud)

c++ g++ stdmap

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

使用std :: map时无法捕获未处理的exection

我试图利用operator[]in std::map来使用键读取元素.但是当我试图访问一个无效的密钥时,它会抛出一个我无法使用的异常try- catch阻塞.这是我正在使用的代码:

class MapElement
{
 public:
     int a;
     char c;
};

int main()
{
   MapElement m1,m2,m3;
   m1.a =10;m1.c = 'a';
   m2.a =20;m2.c ='b';
   m3.a =30;m3.c ='c';

   map <char ,MapElement*> Mymap;
   map<char,MapElement*>::iterator iter = Mymap.begin();
   Mymap.insert(iter , std::pair<int, MapElement*>('1',&m1));
   Mymap.insert(iter , std::pair<int, MapElement*>('1',&m2));
   cout<<Mymap['1']->a;
   try
   {
      cout<<Mymap['2']->a;
   }
   catch(exception e)
   {
       cout<<e.what();
   }
   catch(...)
   {
        cout<< "unknown error";
   }
}
Run Code Online (Sandbox Code Playgroud)

我该如何处理这个例外?

c++ stl exception stdmap

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

std :: map是否可以包含对构造函数的引用?

有没有办法从std :: map指向构造函数?我想用我想要使用的代码执行以下操作,#if 0但我似乎无法使其工作:

#include <map>
#include <functional>

using namespace std;

class Base { };
class A : public Base { };
class B : public Base { };

enum class Type { A, B, };

#if 0
using type_map_t = std::map<Type, std::function<Base*()>>;
type_map_t type_map = {
    {Type::A, &A::A},
    {Type::B, &B::B},
};
#endif

Base*
getBase(Type t)
{
#if 0
    auto constructor = type_map[t];
    return constructor();
#else
    switch(t)
    {
        case Type::A:
            return new A();
        case Type::B:
            return new B(); …
Run Code Online (Sandbox Code Playgroud)

c++ stdmap c++11 std-function

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

与std :: map相比的操作数顺序

说我有:

bool operator<(Type const& lhs, Type const& rhs) { /* ... */ }
std::map<Type, void*> m;
Run Code Online (Sandbox Code Playgroud)

如果我现在这样做

Type t{};
m.find(t);
Run Code Online (Sandbox Code Playgroud)

t总是在左侧的参数中结束,operator<并将m它的元素与右侧的参数进行比较(反之亦然)?

或者这种实现是否依赖,可以采用哪种方式?

c++ stdmap language-lawyer

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

为什么我可以将std :: map的键传递给期望非const的函数?

根据std::map 文档,它存储键值对std::pair<const Key, Value>,因此映射中的键是const.

现在假设我有一个std::map键是指向某些对象的指针.

struct S {};
struct Data {};
using MyMap = std::map<S*, Data>;
Run Code Online (Sandbox Code Playgroud)

我们还假设有一个foo接受S*参数的函数.

void foo(S* ptr) { /* modify the object (*ptr) */ }
Run Code Online (Sandbox Code Playgroud)

现在,问题是:当我MyMap使用基于范围的for循环迭代时,我能够将map元素键传递给foo:

MyMap m = getMyMapSomehow();
for (auto elem : m)
{
    static_assert(std::is_const<decltype(elem.first)>::value, "Supposed to be `const S*`");
    foo(elem.first); // why does it compile?
}
Run Code Online (Sandbox Code Playgroud)

所以,即使我的static_assert成功(所以我假设它的类型elem.firstconst S*),foo编译的调用很好,因此看起来好像我能够修改指针到const后面的对象.

为什么我能做到这一点?

PS这是Coliru …

c++ pointers stl const stdmap

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

迭代映射并使用该对作为参考参数C​​ ++ 11

我有一个std::map.我想迭代它并使用结果作为函数的参数.编译似乎抱怨我的对象是左值,但我无法弄清楚为什么它被认为是左值.

void my_function(std::pair<std::string, std::string>& my_arg){
    //do stuff, modify elements from the pair
}

std::map<std::string, std::string> my_map;
// fill the map with values...

for(auto& element : my_map){
    my_function(element);
}
Run Code Online (Sandbox Code Playgroud)

我可能会使用迭代器来解决这个问题,但我想学习如何用c ++ 11方法来解决这个问题.

c++ stdmap auto c++11

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