标签: stdmap

如何修改std :: map容器中的键值

特定

std::map<int,std::string> myMap;
fillMyMapWithStuff(myMap);

// modify key values - I need to add a constant value to each key
for (std::map<int,std::string>::iterator mi=myMap.begin(); mi != myMap.end(); ++mi)
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

什么是一个好方法应用一些重新索引?我必须删除旧条目并使用新密钥和旧值添加新条目吗?

c++ stdmap

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

使用const char*literal作为std :: map键是否安全?

我有课,每个都返回它的名字

struct IFoo {
    virtual const char * GetName() const = 0;
}

struct Foo : IFoo {
   const char * GetName() const { return "Foo"; }
}

struct Bar: IFoo {
   const char * GetName() const { return "Bar"; }
}
Run Code Online (Sandbox Code Playgroud)

而在其他地方:

Foo* a = new Foo();
Foo* b = new Foo();

std::map<const char *, int> data;
data[a->GetName()] = 0;
printf("%i", data[b->GetName()]);
Run Code Online (Sandbox Code Playgroud)

字符串文字应存储在内存中的一个位置,但它是100%吗?这段代码适用于gcc,但我不确定它的多平台性.

c++ key stdmap string-literals c++11

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

如何在std :: map中使用struct作为键

我想使用std::map其键和值元素是结构的.

我收到以下错误: error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const GUID

我明白我应该operator <为这种情况重载,但问题是我无法访问我想要使用的结构的代码(GUIDVC++中的结构).

这是代码片段:

//.h

#include <map>
using namespace std;

map<GUID,GUID> mapGUID;


//.cpp

GUID tempObj1, tempObj2;              
mapGUID.insert( pair<GUID,GUID>(tempObj1, tempObj2) );   
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题呢?

c++ stl visual-studio-2005 stdmap

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

擦除元素时,映射迭代器如何失效?

使用擦除方法时,地图中的迭代器何时以及如何失效?

例如 :

std :: map < int , int > aMap ;

aMap [ 33 ] = 1 ;
aMap [ 42 ] = 10000 ;
aMap [ 69 ] = 100 ;
aMap [ 666 ] = -1 ;

std :: map < int , int > :: iterator itEnd = aMap.lower_bound ( 50 ) ;

for ( std :: map < int , int > :: iterator it = aMap.begin ( ) ;
      it != itEnd ; …
Run Code Online (Sandbox Code Playgroud)

c++ iterator stl stdmap std

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

您可以创建继承类的 std::map 吗?

我想知道是否可以创建继承类的指针映射。这是我正在尝试做的事情的一个例子:

#include <string>
#include <map>

using namespace std;

class BaseClass
{
    string s;
};

class Derived1 : public BaseClass
{
    int i;
};

class Derived2 : public Derived1
{
    float f;
};

// Here's what I was trying, but isn't working
template<class myClass>
map<string, myClass>m;

int main()
{
    // Add BaseClasses, Derived1's, and/or Derived2's to m here
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

main.cpp(23):错误 C2133:“m”:未知大小
main.cpp(23): 错误 C2998: 'std::map<std::string,myClass>m' :不能是模板定义

我明白为什么会出现此错误,但我想知道是否可以创建一个可以保存不同级别的继承类的映射?如果没有,是否可以创建某种可以容纳各种类别类型的管理系统?或者我必须制作不同的地图/向量/数组/等。对于每种类型的课程?

c++ inheritance stdmap

6
推荐指数
2
解决办法
9422
查看次数

字符数组作为C++映射中的值

我想定义类似的东西

Map<int, char[5] > myMap;
Run Code Online (Sandbox Code Playgroud)

上面的声明被c ++编译器接受,并且没有抛出错误,但是当我做这样的事情时

int main()
{
    char arr[5] ="sdf";
    map <int, char[5]> myMap;
    myMap.insert(pair<int, char[5]>(0,arr));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到错误:

In file included from /usr/include/c++/4.6/bits/stl_algobase.h:65:0,
                 from /usr/include/c++/4.6/bits/char_traits.h:41,
                 from /usr/include/c++/4.6/ios:41,
                 from /usr/include/c++/4.6/ostream:40,
                 from /usr/include/c++/4.6/iostream:40,
                 from charMap.cpp:1:
/usr/include/c++/4.6/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = int, _T2 = char [5]]’:
charMap.cpp:9:42:   instantiated from here
/usr/include/c++/4.6/bits/stl_pair.h:104:31: error: array used as initializer
/usr/include/c++/4.6/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = int, _U2 = char [5], _T1 …
Run Code Online (Sandbox Code Playgroud)

c++ dictionary stdmap std c++-standard-library

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

将元素插入std :: map而无需额外复制

考虑这个程序:

#include <map>
#include <string>
#define log magic_log_function // Please don't mind this.

//
// ADVENTURES OF PROGO THE C++ PROGRAM
//

class element;
typedef std::map<int, element> map_t;

class element {
public:
    element(const std::string&);
    element(const element&);
    ~element();
    std::string name;
};
element::element(const std::string& arg)
    : name(arg)
{
    log("element ", arg, " constucted, ", this);
}
element::element(const element& other)
    : name(other.name)
{
    name += "-copy";
    log("element ", name, " copied, ", this);
}
element::~element()
{
    log("element ", name, " destructed, ", this); …
Run Code Online (Sandbox Code Playgroud)

c++ optimization stdmap c++-standard-library

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

从std :: map值获取密钥的有效方法

我有一张地图如下:

std::map< std::string ,int> mapobj;
mapobj["one"] = 1;
mapobj["two"] = 2;
mapobj["three"] =3 ;
Run Code Online (Sandbox Code Playgroud)

输入值时如何获取键

EX:

输入:1

输出:一

注意:在我的情况下,值是唯一的

c++ performance stdmap

6
推荐指数
2
解决办法
1408
查看次数

为什么std :: map有一个名为count的成员函数?

我正在学习C++,显然是一种检查a中是否存在特定键的方法std::map是使用成员函数count.

我的第一个问题是:键不应该是唯一的吗?确实检查文档它们是唯一的,因此count将返回0或1.

打电话不是有点违反直觉count吗?为什么不exist呢?

对于我来说,计数在一个列表中是有意义的,在该列表中您期望出现一些元素,但如果该方法只允许返回1或0,则对我来说没有意义.

我错过了什么吗?是否有理由将其称为count或者只是一个糟糕的命名?

c++ stdmap std

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

如何使用 C++11 声明静态查找表

我有 C++11 程序,它需要从一系列值中选择一个值。每个范围都有一个指定的值以从中选择一个值。

以下是我的值范围,并为每个值分配了值。

1-10 = 5
11-14 = 13
15-20 = 17
21-28 = 24
29-36 = 31
37-47 = 43
48-52 = 50
53-60 = 53
61-68 = 65
Run Code Online (Sandbox Code Playgroud)

因此,如您所见,如果输入介于 1-10 之间,则 5 应为返回值,如果输入介于 11-14 之间,则应选择 13,依此类推。

上面的要求可以通过下面的程序来实现,其中包含一个很大的 if else 语句的脏列表。

#include <iostream>

// Following are return values to chosen based on which range the input value falls within
// 1-10 = 5
// 11-14 = 13
// 15-20 = 17
// 21-28 = 24
// 29-36 = …
Run Code Online (Sandbox Code Playgroud)

c++ stdmap lookup-tables c++11 std-pair

6
推荐指数
2
解决办法
982
查看次数