我有一个全局对象,包含几个c ++ std :: map对象.该对象应该只在多线程环境中读取.但是,当在std :: map的实现中读取C++ std :: map对象时,我不确定是否存在任何写操作.IDE是Visual Studio 2008.我应该为读取操作提供一些同步机制吗?
我正在为我的游戏引擎构建一个内容存储系统,我正在寻找存储数据的可能替代方案.由于这是一款游戏,因此表现很重要.特别是考虑到引擎中的各种实体将在内容管理器创建时从内容管理器的数据结构请求资源.我希望能够通过名称而不是索引号来搜索资源,因此某种字典是合适的.
使用std :: map并基于std :: vector创建自己的字典类有什么优缺点?是否有任何速度差异(如果是这样,性能会在哪里受到影响?即附加与访问)并且是否有时间花时间写我自己的课程?
有关需要发生的事情的一些背景信息:只有在引擎加载时才会写入数据结构.所以在游戏过程中实际上没有写作.当引擎退出时,要清理这些数据结构.无论何时创建实体或交换地图,都可以随时读取它们.一次只能创建一个实体,或者多达20个实体,每个实体都需要可变数量的资源.资源大小也可能根据引擎启动时读取的文件大小而变化,图像最小,音乐最大,具体取决于格式(.ogg或.midi).
有:
std::map<const std::string,A > cache;
Run Code Online (Sandbox Code Playgroud)
你将如何插入这个容器(可以重复尝试):
cache.insert(std::make_pair(id,ps));
cache.insert(std::pair<std::string,A>(id,ps));
if(cache.find(id) == cache.end()){
cache[id] = ps;
}
Run Code Online (Sandbox Code Playgroud)
为什么??(在时间和记忆方面)
你有更好的解决方案吗?
更新:我没有使用C++ 11
Update-2: 好的,到目前为止我们意识到:
make_pair并且pair<>是类似的.
insert和[ ](有或没有if检查)都会调用copy.那么......之间的竞争是:
insert [ ](带if检查)[ ](带if检查和交换)你更喜欢哪一个?
再次感谢
我的问题很简单.我google了很多,但不知怎的,我无法弄明白.我使用的是C++ std::map有std::unique_pointer这样的:
std::unique_ptr<std::map<int,std::string>> my_map( new std::map<int,std::string>());
Run Code Online (Sandbox Code Playgroud)
现在,我想使用[]地图的访问运算符.但我总是遇到编译器错误.
my_map[1] = "XYZ"; // error C2676
my_map->[1] = "XYZ"; // error C2059
Run Code Online (Sandbox Code Playgroud)
如果没有std::unique_ptr,我的代码将如下所示,并且它的工作原理.但是我如何做同样的事情std::unique_ptr呢?请帮我.
std::map<int,std::string> my_map;
my_map[1] = "XYZ"; // OK!
Run Code Online (Sandbox Code Playgroud)
现代C++是受欢迎的,甚至是期望的.
我有一个方法在地图中找到特定的位置,并通过迭代器引用'返回':
bool Func(const int searchKey, MyMap::iterator& iter) const {
iter = _map.upper_bound(searchKey); // Compiler error: comparing non-const iterator and const iterator
const bool found = iter != _map.begin();
if(something){
--_map;
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我收到编译器错误,因为std::upper_bound()它返回std::const_iterator并将其与a进行比较std::iterator.
我应该'转换'非常量的返回值upper_bound()吗?
我有一个std::map对象.键是实体ID(整数)并且它们的2D位置(向量)的值.目的是确定哪些实体处于相同的位置.
ID Position
1 {2,3}
5 {6,2}
12 {2,3}
54 {4,4}
92 {6,2}
Run Code Online (Sandbox Code Playgroud)
我需要得到一个由键组成的向量矢量,它们具有相同的值.
以上示例输入数据的输出:{1,12},{5,92}
我知道我可以将2D位置复制到向量矢量并循环第一级向量以找到相等的第二级向量的索引.然后通过索引选择向量并再次循环查找相应的键来返回查找键.
请为此建议更清洁的方法.
我使用polinoms并将它们作为度和系数保存在std :: map中.这是代码片段:
std::map<int,int> pol;
Run Code Online (Sandbox Code Playgroud)
地图填充了数据,然后我开始处理它.
for(std::map<int,int>::iterator it = pol.begin(); it != pol.end(); it++) {
if( it->first != 0 ) {
it->second *= it->first;
it->first--;
}
else {
it->first = 0;
it->second = 0;
}
}
Run Code Online (Sandbox Code Playgroud)
从它开始 - >第一次 - 进一步我得到了非常大的输出,有错误error: decrement of read-only member ‘std::pair<const int, int>::first’
it->first--;
^~
或者error: assignment of read-only member ‘std::pair<const int, int>::first’
it->first = it->first - 1;
为什么它是只读?我该如何解决?
$ g++ --version
g++ (Debian 6.3.0-5) 6.3.0 20170124
Run Code Online (Sandbox Code Playgroud) #include<bits/stdc++.h>
#define mpq pair<int,Query>
using namespace std;
class Query{
public:
int a1,a2,b1,b2,c1,c2,d,l,r;
Query(){}
Query(int a1,int a2,int b1,int b2,int c1,int c2,int d,int l,int r){
this->a1=a1; this->a2=a2;
this->b1=b1; this->b2=b2;
this->c1=c1; this->c2=c2;
this->d=d; this->l=l; this->r=r;
print();
}
Query(int d,int l,int r){
Query(0,1,0,1,0,1,d,l,r);
}
void print(){
cout<<d<<" "<<l<<" "<<r<<endl;
}
};
map<int,Query> query;
int main(){
query.insert(mpq(1,Query(0,1,0,1,0,1,1,1,1)));
query[1].print();
cout<<endl;
query[4]=Query(4,4,4);//not working properly
query[4].print();//giving output of query[1].print
cout<<endl;
query[2]=Query(0,1,0,1,0,1,2,2,2);
query[2].print();
}
Run Code Online (Sandbox Code Playgroud)
我无法解释此代码的输出
query[4].print()是否给出了意外的输出
我期待输出
4 4 4
但它正在给予
1 1 1
作为输出
采取以下简单程序:
struct Foo
{
int x;
int y;
int z;
string s;
};
int main()
{
Foo f1 = { 42,21,11, "Hello world" };
std::map<int, Foo> foomap;
foomap[400] = f1;
Foo* ptr = &foomap[400]; // cache a pointer to the element we just inserted.
cout << ptr->x << " " << ptr->y << " " << ptr->z << " " << ptr->s << std::endl;
// fill the map up with a bunch of other random items at random indices
for …Run Code Online (Sandbox Code Playgroud) 我有一个动画,在默认构造函数,析构函数和另一个构造函数中打印一些独特的字符串:
class Animation{
public:
int x;
Animation(int x) {
std::cout << "+ animation\n";
}
Animation() {
std::cout << "+ animation\n";
}
~Animation() {
std::cout << "- animation\n";
}
}
Run Code Online (Sandbox Code Playgroud)
我想用这个对象填充std :: map,std :: map定义是这样的:
std::map<int, Animation> animations;
Run Code Online (Sandbox Code Playgroud)
当我尝试填充地图时,我这样做
void createAnimations(){
animations[0] = Animation(10);
animations[1] = Animation(10);
animations[2] = Animation(10);
animations[3] = Animation(10);
animations[4] = Animation(10);
}
Run Code Online (Sandbox Code Playgroud)
当我运行程序时,打印出来
+ *animation
+ animation
- animation
+ *animation
+ animation
- animation
+ *animation
+ animation
- animation
+ *animation
+ animation
- …Run Code Online (Sandbox Code Playgroud) c++ ×10
stdmap ×10
c++11 ×2
dictionary ×2
std ×2
stl ×2
algorithm ×1
c++03 ×1
c++14 ×1
const ×1
constructor ×1
duplicates ×1
iterator ×1
performance ×1
unique-ptr ×1
vector ×1