如何为多图中的特定键选择随机元素.例如:
multimap<string, string> map;
map.insert(pair<string, string>("Mammal", "Tiger"));
map.insert(pair<string, string>("Mammal", "Chicken"));
map.insert(pair<string, string>("Mammal", "Fox"));
map.insert(pair<string, string>("Fish", "Clown Fish"));
map.insert(pair<string, string>("Fish", "Ray"));
Run Code Online (Sandbox Code Playgroud)
在上面,获得随机"哺乳动物"的最佳方法是什么?
我知道我可以获得"哺乳动物"的迭代器所以:
pair<MultiMapIt,MultiMapIt>iterators = mMultiMap.equal_range("Mammal");
// loop through each... and select one.
Run Code Online (Sandbox Code Playgroud)
但我相信有更好的解决方案......也许使用迭代器作为数字..
谢谢
我有一个std::map的HANDLE对象,
std::map<int, HANDLE> MyMap;
我必须使用WaitForMultipleObjects()函数等待这些句柄对象.为此,我必须将地图内容作为HANDLE对象的数组.(如果它是一个向量,那么我们可以使用vector.data()函数).我正在寻找这样一个简单的功能来实现这一目标.
C++ std :: priority_queue只需要一个部分顺序.但如果它的实现是二进制堆,它是如何工作的?例如:假设我们有一个偏序集( {a, b, c, x}, {c < b, b < a, c < a} ),x具有无关a,b,c.那么最大堆是:
layer 1: x
layer 2: b x
layer 3: x x a c
Run Code Online (Sandbox Code Playgroud)
在弹出操作之后,以教科书中常见的方式,即用root替换root c并将大小减小1.然后我们需要在下面的树根下堆积树:
layer 1: c
layer 2: b x
layer 3: x x a
Run Code Online (Sandbox Code Playgroud)
我们会互换c,b因为c < b,不是吗?什么?从那以后我们仍然没有有效的堆b < a.但b不能"看" a.
我正在尝试映射::插入一个我定义的关键数据类型(miniVec2ui)..它在比较期间中断(比较时它有伪造的数据)..它使用标准的较少的仿函数(我尝试用我自己的,但同样的问题) - 它通过rhs.x = ??? 和rhs.y = ??? 基本上.代码如下
miniVec2ui v233 = miniVec2ui(x,y);
m_pIdMap->insert(std::pair<miniVec2ui,uint>(v233,(uint)tmpPF.id));// std::pair<const miniVec2ui,uint>(v233, (uint)tmpPF.id) );
//////in other files I defined miniVec2ui,etc. (uint = unsigned int), tmpPF.id unimportant here, uint x; uint y;
typedef std::map<miniVec2ui,uint> eIdMap;
eIdMap *m_pIdMap;
m_pIdMap = new eIdMap;//new std::map<miniVec2ui, uint >;
struct miniVec2ui {
uint x;
uint y;
miniVec2ui(uint inx, uint iny):
x(inx)
,y(iny) { }
bool operator==(const miniVec2ui& rhs) const {
return ((x == rhs.x) && (y == rhs.y));
}
bool operator!=(const miniVec2ui& …Run Code Online (Sandbox Code Playgroud) 可能重复:
std :: ifstream类的设计
为什么(i | o)fstream采用const char*参数作为文件名?
看一下std :: ifstream的构造函数,我发现两个:
ifstream ( );
explicit ifstream ( const char * filename, ios_base::openmode mode = ios_base::in );
Run Code Online (Sandbox Code Playgroud)
为什么第二个拿一个const char *而不是一个const std::string &?
这是某种避免循环或前向参考吗?
目前我正在努力应对包含自己的地图.但是编译时我不知道嵌套的深度.
std::map<Key, std::map<Key, std::map<Key, std::map<Key, ...>>>>
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这个目标而不是无限重复自己?
我见过std::string在.c文件中使用过.std是一个c ++命名空间,命名空间是在c ++中引入的.为什么会这样?它不应该抛出错误吗?
最近我一直在尝试更新一些代码以利用标准的C++库函数而不是旧的C风格函数.特别是,我试图做以下(简单的人工工作示例 - 我知道代码很丑,但它简洁地说明了问题):
std::vector<int> vData;
vData.push_back(10990);
vData.push_back(11990);
vData.push_back(12990);
vData.push_back(13990);
unsigned char szBuffer[100];
memset(szBuffer,0,sizeof(szBuffer));
std::copy(vData.begin(),vData.end(),szBuffer);
Run Code Online (Sandbox Code Playgroud)
我原以为这会像我想要替换的代码一样:
memcpy(szBuffer,&vData[0],sizeof(int)*vData.size());
Run Code Online (Sandbox Code Playgroud)
但是调试代码很明显,std::copy我编写的代码只是写入unsigned char缓冲区的前4个字节而不是向量中4个整数的完整位模式.有人可以告诉我我做错了什么,或者只是我不能std::copy以这种方式使用并且应该坚持下去memcpy?
很长一段时间我都在使用return 0;,从我开始使用allegro开始.比起我看过C++书并找到关于EXIT_SUCCESS的内容,比我查看定义的stdlib.h或cstdlib并且只有两行:
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释一下吗?我正在使用最后一个devc ++ 4.9.9.2的mingw版本.
(我正在使用Visual C++ + 2010)假设我已经定义了这样的元组:
typedef std::tr1::tuple<
int //i want to set its default value to 9
, double //i want to set its default value to 3.3
, int //i want to set its default value to 2
, double //i want to set its default value to -7.2
> Mytuple;
Run Code Online (Sandbox Code Playgroud)
我可以在结构中做到这一点.但我想知道是否可以这样做std::tr1::tuple.
此外,我想知道什么时候使用std::tr1:tuple或者struct?
有人可以帮帮我吗?