我需要一个像C++ std :: map一样工作的类.更具体地说,我需要这样的行为:
map< string, vector<int> > my_map;
这可能吗?
我有一个枚举StackIndex定义如下:
typedef enum
{
DECK,
HAND,
CASCADE1,
...
NO_SUCH_STACK
} StackIndex;
Run Code Online (Sandbox Code Playgroud)
我创建了一个名为的类MoveSequence,它是std::deque表单中一堆元组的包装器<StackIndex, StackIndex>.
class MoveSequence
{
public:
void AddMove( const tpl_move & move ){ _m_deque.push_back( move ); }
void Print();
protected:
deque<tpl_move> _m_deque;
};
Run Code Online (Sandbox Code Playgroud)
我以为我可以创建一个类的静态std::map成员MoveSequence,它将a转换StackIndex为a std::string,供Print()函数使用.但是当我尝试时,我收到了错误:
"error C2864: 'MoveSequence::m' : only static const integral data members can be initialized within a class"
Run Code Online (Sandbox Code Playgroud)
如果不能将std :: map创建为静态成员,是否有另一种方法可以创建std :: map,将a StackIndex转换为std::string可用于打印MoveSequence对象的a?
谢谢
Beeband.
我的问题几乎与这个问题相同,但那里的解决方案还没有解决我的错误.
在main.h我有:
#include <map>
#include <string>
std::map<std::string, int64_t> receive_times;
Run Code Online (Sandbox Code Playgroud)
并在main.cpp:
std::map<std::string, int64_t>::const_iterator iter;
std::map<std::string, int64_t>::const_iterator eiter = receive_times.end();
for (iter = receive_times.begin(); iter < eiter; ++iter)
printf("%s: %ld\n", iter->first.c_str(), iter->second);
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试编译时,我收到以下错误:
error: invalid operands to binary expression ('std::map<std::string, int64_t>::const_iterator' (aka '_Rb_tree_const_iterator<value_type>') and 'std::map<std::string, int64_t>::const_iterator'
(aka '_Rb_tree_const_iterator<value_type>'))
for (iter = receive_times.begin(); iter < eiter; ++iter)
~~~~ ^ ~~~~~
Run Code Online (Sandbox Code Playgroud)
我在顶部链接的问题中的解决方案是因为缺少了#include <string>,但显然我已将其包括在内.任何提示?
我指的是一个比较qsort vs stdsort性能的上一个链接.
我写了一个C程序,填充了一个大的std::map,我想对数组进行排序qsort.我正在使用.
typedef std::map<uint16_t, uint32_t> TSrcMap;
TPSrcMap sp;
TSrcMap::iterator its;
/*Code to populate the array_start.*/
/*Code to populate the array_end.*/
typedef struct port_count
{
uint32_t port_number;
uint32_t port_count;
}port_count_t;
port_count_t pcount[10];
memset(pcount,0,sizeof(pcount));
size_t structs_len = sizeof(pcount)/sizeof(port_count_t);
for(its = stcp.begin(); its != stcp.end();its++)
{
if(pcount[smallest_index].port_count < (*its).second)
{
pcount[smallest_index].port_count = (*its).second;
pcount[smallest_index].port_number = (*its).first;
/*qsort(pcount, structs_len, sizeof(port_count_t), struct_cmp_by_port_count);*/
std::sort(pcount,sizeof(port_count_t));
}
}
Run Code Online (Sandbox Code Playgroud)
该qsort函数正确排序数组.我想比较qsortwith 的性能,std::sort但调用std::sort调用给出了编译错误
没有匹配的呼叫功能
‘sort(port_count_t [10], …
我想在C++中存储具有自定义比较器功能的对象std::map,例如:
std::map<Part, Inventory, PartCmp>
Run Code Online (Sandbox Code Playgroud)
对于比较器,我想通过一个计算成本昂贵的"密钥"对对象进行排序,所以我想到了一种懒惰的评估方法.下面的例子有点微不足道,但说明了问题:
class Part {
public:
std::string item_id;
int color_id;
int condition;
std::string name;
std::string category;
std::string key();
private:
std::string key_;
}
std::string Part::key() {
// Only create key value if it hasn't been done before
if (key_.empty()) {
ostringstream keystream;
keystream << item_id << color_id << condition;
key_ = keystream.str();
}
return key_;
}
Run Code Online (Sandbox Code Playgroud)
这意味着我的比较器看起来像:
struct PartCmp {
bool operator() (Part& p1, Part& p2) const {
return p1.key() < p2.key();
}
};
Run Code Online (Sandbox Code Playgroud)
这与我见过的所有其他示例不同,p1 …
在我不应该用-std = c ++ 11编译的限制下,我想在以下变量中添加一个额外的变量,
std::map<unsigned,
std::map<unsigned,
std::pair<
std::pair<int, int>,
std::pair<bool, bool>
>
>
>
temp;
Run Code Online (Sandbox Code Playgroud)
并据此调整其初始化:
for (int i=0; i<100; ++i)
{
temp[i][i]=
std::pair<
std::pair<int, int>,
std::pair<bool, bool>
>
> (
std::pair<int, int> (intsX[i], intsY[i]),
std::pair<bool, bool> (boolsX[i], boolsY[i])
);
}
Run Code Online (Sandbox Code Playgroud)
我想将结构更改为:
std::map<unsigned,
std::map<unsigned,
std::pair<
std::pair<int, int>,
std::pair<
std::pair<bool, bool>,
double
>
>
>
>
temp;
Run Code Online (Sandbox Code Playgroud)
现在,我不知道如何更改初始化,例如......
for (int i=0; i<100; ++i)
{
temp[i][i]=
std::pair<
std::pair<int, int>,
std::pair<
std::pair<bool, bool>,
double
>
> (
std::pair<int, int> (intsX[i], intsY[i]),
std::pair<std::pair, …Run Code Online (Sandbox Code Playgroud) 我试图使用固定长度的数组作为a的键std::map,如下面的非编译代码所示:
#include <cstdlib>
#include <iostream>
#include <map>
typedef char myuuid[ 16 ];
template <class T>
class MyAlloc
{
public:
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef const T* const_pointer;
typedef const T& const_reference;
template <class U> struct rebind { typedef MyAlloc<U> other; };
MyAlloc() {}
MyAlloc( const MyAlloc& other ) {}
T* allocate( std::size_t n ) { return static_cast<T*>( std::malloc( n * sizeof( value_type ) ) ); }
void deallocate( …Run Code Online (Sandbox Code Playgroud) 我正在使用std :: map,无法理解它消耗了多少内存。
我有以下地图定义:
CKey {
long x;
int y;
int z;
bool operator<(const CKey& l) const;
};
CValue {
int data1;
int data2;
}
std::map<CKey, CValue> map;
std::cout << "sizeof() = " << sizeof(map) << " Max #Elms = " << map.max_size();
Run Code Online (Sandbox Code Playgroud)
(在地图中插入元素之前或之后都没有问题)
sizeof() = 48
Max_Size = 329406144173384850
Run Code Online (Sandbox Code Playgroud)
编辑:第一个问题的答案是使用std :: pair.关于第二个的任何想法(标记为'奖金问题')?
使用以下代码:
#include <map>
#include <vector>
void foo(std::pair<int, int>& p) // EDIT: it needs to be non-const
{}
int main()
{
std::pair<int, int> p{1,2};
foo(p);
std::vector<std::pair<int, int>> v{{1,2}};
for (auto& element : v)
{
foo(element); // works fine
}
std::map<int, int> m{std::make_pair(1,2)};
//std::map<int, int> m2{{1,2}};
for (auto& element : m) // the problematic loop
{
foo(element);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在后者for循环中得到以下消息:
错误:从'std :: pair'类型的表达式初始化'std :: pair&'类型的引用无效
以及m2中的以下内容:
错误:从'std :: pair'类型的右值开始无效初始化'std :: pair&'类型的非const引用
这是为什么?
额外的问题: 我发现非常奇怪的是,当m2的初始化没有被注释掉并且for循环保持不变(其中仍有m并且m2从未使用过)时,错误消息从
错误:从'std :: pair'类型的表达式初始化'std …
该snmp.h头文件包含的定义,AsnObjectIdentifier结构和遗憾的是这个结构不相等运算符重载。我想AsnObjectIdentifier成为的钥匙,std::map但问题find()是无法在地图上找到钥匙。我定义了一个自定义比较器AsnObjectIdentifierComparator,用作std :: map声明的第三个模板参数。该方案的最小可复制代码如下:
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef unsigned int UINT;
typedef struct {
UINT idLength;
UINT * ids;
} AsnObjectIdentifier;
struct AsnObjectIdentifierComparator {
bool operator()(const AsnObjectIdentifier& left, const AsnObjectIdentifier& right) const {
UINT* leftOidArr = left.ids, * rightOidArr = right.ids;
UINT smallerOidLen = (left.idLength < right.idLength ? left.idLength : right.idLength);
for (UINT i = 0; i < smallerOidLen; i++) {
if (leftOidArr[i] …Run Code Online (Sandbox Code Playgroud)