使用C++和boost.在Boost.Assign中,我可以将new运算符与map_list_of一起使用吗?
例如:
std::map<int, MyObject*> objects = boost::assign::map_list_of
(1, new MyObject())(2, new MyObject())(3, new MyObject())
Run Code Online (Sandbox Code Playgroud)
如果没有,还有另一种方法吗?
我在这里读到,如果密钥不存在,std :: map operator []会创建一个对象!
首先,我可以知道在哪里可以找到这个索赔的参考资料吗?(虽然我知道这是真的)
接下来,想象下面的代码片段:
#include <iostream>
#include <vector>
#include<map>
class Value {
//..
int some_member; //is used for any purpose that you like
std::vector<int> some_container;
public:
Value(int some_member_) :
some_member(some_member_) {
std::cout << "Hello from the one-argument constructor" << std::endl;
}
Value() {
std::cout << "Hello from the no argument constructor" << std::endl;
}
void add(int v) {
some_container.push_back(v);
}
int getContainerSize()
{
return some_container.size();
}
//...
};
//and somewhere in …Run Code Online (Sandbox Code Playgroud) 考虑以下C++代码:
// A.h
class A {
private:
std::map<int, int> m;
int getValue(int key) const;
};
// A.cpp
int A::getValue(int key) const {
// build error:
// No viable overloaded operator[] for type 'const std::map<int, int>'
return m[key];
}
Run Code Online (Sandbox Code Playgroud)
如何从函数m上下文中获取值const?
在test.h:
#ifndef TEST_H
#define TEST_H
#include <map>
struct Incomplete;
class Test {
std::map<int, Incomplete> member;
public:
Test();
int foo() { return 0; }
};
#endif
Run Code Online (Sandbox Code Playgroud)
在test.cpp:
#include "test.h"
struct Incomplete {};
Test::Test() {}
Run Code Online (Sandbox Code Playgroud)
在 main.cpp
#include "test.h"
int main() {
Test test;
return test.foo();
}
Run Code Online (Sandbox Code Playgroud)
g ++ 4.7给我一个错误,该错误struct Incomplete在我编写时已预先声明g++ main.cpp test.h -o main.o。
但是,如果更改std::map<int, Incomplete> member为std::map<int, Incomplete*> member,则会进行main.o编译。为什么是这样?
我有一个结构
struct key
{
int x;
int y;
int z;
};
Run Code Online (Sandbox Code Playgroud)
比如x,y,z可以取1到10之间的值.
我也有一张地图
std::map<key,double> myMap;
Run Code Online (Sandbox Code Playgroud)
我用不同的键值填充.
有没有办法循环遍历所有键值,例如z = 5.那是(就伪代码而言)
loop over myMap
double v += myMap.find({x=anything,y=anything,z=5})->second;
Run Code Online (Sandbox Code Playgroud)
如果有人可以提供一些关于这是否可以实现的评论(我不想使用增强容器),那将是非常友好的.
我(向前)遍历std :: map,并希望查找迭代器是否指向倒数第二个元素。我似乎找不到任何地方的方法。
我有:
bool
isSecondLastFile(const TDateFileInfoMap::const_iterator &tsFile)
{
TDateFileInfoMap::reverse_iterator secondLastIt = mFileInfoMap.rbegin() + 1;
return (tsFile == secondLastIt);
}
Run Code Online (Sandbox Code Playgroud)
TDateFileInfoMapstd :: map 在哪里
我越来越:
error: no match for ‘operator==’ in ‘tsFile == secondLastIt’
/usr/lib/gcc/i686-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_tree.h:287: note: candidates are: bool std::_Rb_tree_const_iterator<_Tp>::operator==(const std::_Rb_tree_const_iterator<_Tp>&) const [with _Tp = std::pair<const long int, TFileInfo>]
Run Code Online (Sandbox Code Playgroud)
这是否意味着我无法比较正向和反向迭代器?
我如何确定正向迭代器是否指向倒数第二个元素?
我的数据将存储在整数和整数的映射中.键是任何数字的start_range.值是end_range
例如,我的地图将如下所示:
std::map<int,int> mymap;
mymap[100]=200;
mymap[1000]=2000;
mymap[2000]=2500;
mymap[3000]=4000;
mymap[5000]=5100;
Run Code Online (Sandbox Code Playgroud)
现在,如果我的输入数字是150,那么算法应该将一个迭代器返回到mymap [100].但是,具有输出值(即迭代器 - >秒)的范围检查逻辑应单独完成,以验证它是否落在正确的范围内.
对于输入数字4500,它可能返回mymap [5000],但范围检查逻辑应该失败,因为它是从5000到5100.请注意地图中没有范围的OVERLAP.
假设我们有一个昂贵的函数映射string到int并希望将结果缓存在地图中.
最简单的代码就是
int mapStringToIntWithCache(std::string const& s) {
static std::unordered_map<std::string, int> cache;
if (cache.count(s) > 0) return cache[s];
else return cache[s] = myExpensiveFunction(s);
}
Run Code Online (Sandbox Code Playgroud)
但这有2次查找.
因此,我倾向于写这个
int mapStringToIntWithCache(std::string const& s) {
static std::unordered_map<std::string, int> cache;
size_t sizeBefore = cache.size();
int& val = cache[s];
if (cache.size() > sizeBefore) val = myExpensiveFunction(s);
return val;
}
Run Code Online (Sandbox Code Playgroud)
这只有一个查找,但似乎有点笨拙.有没有更好的办法?
我尝试编写一个std :: map <Vector3D,double>,其中colinear(并行或反并行)向量应该共享相同的密钥.
作为比较函数,我使用以下函数(在isEqualEnough()中具有1e-9容差),这是我在std :: map中使用(数学)向量创建的
struct Vector3DComparator
{
bool operator() (const Vector3D& lhsIn, const Vector3D& rhsIn) const
{
Vector3D lhs = lhsIn.absolute(); // make all members positive
Vector3D rhs = rhsIn.absolute();
if ((lhs.z < rhs.z))
return true;
if ((isEqualEnough(lhs.z, rhs.z))
&& (lhs.y < rhs.y))
return true;
if ((isEqualEnough(lhs.z, rhs.z))
&& (isEqualEnough(lhs.y, rhs.y))
&& (lhs.x < rhs.x))
return true;
return false;
}
};
Run Code Online (Sandbox Code Playgroud)
当我将一个立方体的法线插入我的地图时,我应该得到3个不同的值(因为我不关心方向)但我得到4:
请使用以下C ++代码段:
#include <map>
int main() {
std::map<int, int> m1;
m1[1] = 2;
std::map<int, int> m2;
m2[3] = 4;
m1.erase(m2.begin());
return m2.size();
}
Run Code Online (Sandbox Code Playgroud)
在Godbolt上:https://godbolt.org/z/mJBszn
感觉必须是不确定的行为。那是对的吗?如果是这样,则该标准的哪一部分如此规定?
c++ ×10
stdmap ×10
dictionary ×2
algorithm ×1
boost ×1
const ×1
constructor ×1
iterator ×1
performance ×1
struct ×1