gameObjects是一个std::map<sf::String,VisibleGameObject*>,results是一个std::map<sf::String,VisibleGameObject*>::iterator.当这运行:
return gameObjects.erase(results);
Run Code Online (Sandbox Code Playgroud)
我期望VisibleGameObject的析构函数运行,这是:
VisibleGameObject::~VisibleGameObject(){
m_pSceneManager->removeSprite(name);
}
Run Code Online (Sandbox Code Playgroud)
永远不会运行,直到持有的类gameObjects被销毁,然后运行:
GameObjectManager::~GameObjectManager(){
std::for_each(gameObjects.begin(),gameObjects.end(),GameObjectDeallocator());
}
struct GameObjectDeallocator{
void operator()(const std::pair<sf::String,VisibleGameObject*>&p) const{
delete p.second;
}
};
Run Code Online (Sandbox Code Playgroud)
然后它确实运行.为什么不在第一种情况下运行?
使用SFML 2.0
谢谢
嗨,我想创建一个带有std :: map的typedef,但编译器返回以下错误:"erro:错误的模板参数数量(2,应为4)"
码:
typedef std::map<unsigned int, float> AcumulatorHash;
Run Code Online (Sandbox Code Playgroud) 我有一个使用std :: map的循环模式.
我只想在键存在时检索值,否则我不想插入元素.目前我正在使用count(key)或find(key)(哪一个更好?从文档中复杂性似乎是相同的),如果它们返回一个正值,我访问地图.但是我想避免在地图上使用两个操作.就像是:
map<string, int> myMap;
int returnvalue;
boole result = myMap.get("key1",returnValue)
if(result){
\\ use returnValue
}
Run Code Online (Sandbox Code Playgroud)
阅读cplusplus.com上的std :: map文档我找到了两个访问地图元素的函数:
他们都不满足我的需要.
我有一张地图:
std::map<std::string, MyDataContainer>
Run Code Online (Sandbox Code Playgroud)
在MyDataContainer一些class或struct(无所谓).现在我想创建一个新的数据容器.假设我想使用默认构造函数来实现它:
// This is valid, MyDataContainer doesn't need constructor arguments
MyDataConstructor example;
// The map definition
std::map<std::string, MyDataContainer> map;
std::string name("entry");
// This copies value of `example`
map[name] = example;
// Below, I want to create entry without copy:
std::string name2 = "nocopy"
// This is pseudo-syntax
map.createEmptyEntry(name2);
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?当我想在地图中初始化它时跳过创建辅助变量?是否有可能使用构造函数参数?
std::map<int, int> m;
// initialize m...
//
int n=3;
for (std::map<int, int>::iterator iter = m.begin()+n; iter != m.end(); ++iter)
// Is the above line correct?
{}
Run Code Online (Sandbox Code Playgroud)
我可以按代码中所示的整数递增迭代器吗?
我有一个以整数向量为键的地图。我用值的关键向量初始化地图{1, 2, 3}
typedef std::map<std::vector<int>, std::string> VectorMap;
VectorMap vectorMap;
vectorMap[std::vector<int>{1, 2, 3}] = "test";
Run Code Online (Sandbox Code Playgroud)
然后我使用count方法来显示是否可以通过向量{1, 2, 3}作为关键字找到VectorMap中的条目。
std::cout << "count: " << vectorMap.count(std::vector<int>{1, 2, 3}) << std::endl;
Run Code Online (Sandbox Code Playgroud)
这将返回正确的计数。
数:1
但是我想这样做,所以向量中整数的顺序无关紧要。所以我尝试与上述相同但矢量内容翻转即{3, 2, 1}
std::cout << "count: " << vectorMap.count(std::vector<int>{3, 2, 1}) << std::endl;
Run Code Online (Sandbox Code Playgroud)
这将返回 0 的计数。
计数:0
我想让矢量比较与内容的顺序无关,只要内容相同。
{1, 2, 3} count: 1
{3, 2, 1} count: 1
{1, 2} count: 0
{1, 2, 3, 4} count : 0
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?我应该完全使用不同的容器而不是 std::vector 吗?
有这个代码:
#include <map>
#include <string>
#include <vector>
void foo1(const std::map<std::string, int> &m)
{
m["abcd"];
}
void foo2(const std::vector<std::vector<int>> &v)
{
v[0];
}
Run Code Online (Sandbox Code Playgroud)
只给出错误foo1,但不给出错误foo2。据我了解map-> 它有 2 个数组 - 一个用于键,第二个用于值。我明白 aconst map给了我const int作为价值。但也应该如此vector<vector<int>>,因为访问元素 via[]也是const vector<int>,但被允许。此外,通过访问值[]并不立即意味着我想要write一个数据。我只能读取那个值,那为什么甚至const map没有operator[]?(当编译器不知道我是想写还是读时)。
问题在于语言设计,而不是标准报价。正如在评论中一样 -> 你需要 1 个操作符来写 map operator[]。但是需要 2 个运算符来进行向量写入operator[], operator=。为什么map::operator[]自动期望我要写?(因此通过提供的键创建新元素)?我可以像在 vector 中一样只是尝试从该映射中读取,如果该键(对)不存在,它会给出错误或警告,但无需立即创建它。
我有一组地图:
std::array<std::map<double, double>, 8> freqMap;
Run Code Online (Sandbox Code Playgroud)
填充它时,我需要在不同的数组索引处向地图添加条目。我知道我可以创建 8 个不同的地图,填充它们,然后将它们添加到数组中,但是是否可以直接在数组中将条目附加到地图?
例如,我如何将着手添加的映射条目key5.0,val3.3至数组索引2,然后添加另一条目数组索引3,然后附加另一条目索引2再次等。
我也可以使用std::vector地图,但仍然看不到以这种方式添加条目的方法。
这是一个例子。我正在从文件中读取数据并想直接更新我的数据结构:
while (fin >> arrayIdx >> key>> val)
freqMaps[arrayIdx] = ??
Run Code Online (Sandbox Code Playgroud) 我想散列一个具有两个私有成员的类,例如:
foo.h
class Foo {
private:
std::string a;
std::string b;
public:
Foo (std::string a, std::string b);
bool operator==(const Foo& other) const;
bool operator!=(const Foo& other) const;
std::size_t operator()(const Foo& ) const;
};
namespace std {
template <> struct hash<Foo> {
std::size_t operator()(const Foo& cp) const;
};
}
Run Code Online (Sandbox Code Playgroud)
文件
Foo::Foo (std::string _a, std::string _b) {
this->a = _a;
this->b = _b;
}
bool Foo::operator== (const Foo& other) const {
return this->a == other.a && this->b == other.b;
}
bool Foo::operator!= (const …Run Code Online (Sandbox Code Playgroud) 我面试过一家跨国公司。他给了我以下代码并要求我使find()函数区分大小写。我尝试过,但无法理解如何使内置查找函数区分大小写。有没有办法使其区分大小写以仅查找特定的键值?
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string, int> mp;
mp["Test"] = 1;
mp["test"] = 2;
mp["TEST"] = 3;
mp["tesT"] = 4;
for (auto it = mp.find("TEST"); it != mp.end(); it++)
{
cout << it->first << " " << it->second << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出 :
TEST 3
Test 1
tesT 4
test 2
Run Code Online (Sandbox Code Playgroud)
但我期望输出是:
TEST 3
Run Code Online (Sandbox Code Playgroud)