考虑以下代码:
#include <boost/serialization/nvp.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
class Foo{
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int)
{
ar & BOOST_SERIALIZATION_NVP(i);
}
int i;
Foo():i(0){}
public:
Foo(int k):i(k){}
};
int main(int argc, char *argv[])
{
std::vector< Foo> f;
f.push_back(Foo(12));
std::ofstream os("path");
boost::archive::xml_oarchive oa(os);
oa << boost::serialization::make_nvp("f", f);
os.close();
std::vector<Foo> g;
std::ifstream is("path");
boost::archive::xml_iarchive ia(is);
ia >> boost::serialization::make_nvp("f", g);
}
Run Code Online (Sandbox Code Playgroud)
在序列化 Foos 向量时效果很好。但是,如果我尝试序列化 Foos 的映射,它会在私有默认构造函数上失败:
std::map<std::string, Foo> f;
f.insert(std::make_pair("hello", Foo(12)));
std::ofstream os("path");
boost::archive::xml_oarchive oa(os);
oa << boost::serialization::make_nvp("f", …Run Code Online (Sandbox Code Playgroud) 我正在做一个简单的地图程序,但最终提出了这个问题。C++ 文档是这样说的:
访问元素 如果 k 与容器中元素的键匹配,则该函数返回对其映射值的引用。如果 k 与容器中任何元素的键都不匹配,则该函数将插入具有该键的新元素并返回对其映射值的引用。请注意,即使没有为元素分配映射值(该元素是使用其默认构造函数构造的),这始终会将容器大小增加一。
我真正不明白的部分是它说“元素是使用其默认构造函数构造的”。
我尝试了一下并做了这个
std::map<string, int> m;
m["toast"];
Run Code Online (Sandbox Code Playgroud)
我只是想看看“toast”的映射元素是什么值。最终结果为零,但是,为什么呢?基本类型有默认构造函数吗?或者发生了什么?
我有一个关于如何找到相应元素的问题,该元素至少有一个数据编号等于在std::mapwith 结构中搜索的键。
例如,假设我要在电话簿中查找一个人的电话号码及其姓名或ID(假设没有一个名字被多个人使用),我将声明一个struct命名Person并定义一个std::map包含Person对象和他的对象/她的电话号码(在 中std::string):
struct Person {
std::string name;
std::string id;
};
std::map<Person, std::string> phonebook;
Run Code Online (Sandbox Code Playgroud)
当我从互联网上搜索时,我知道该结构需要重载==和<运算符来使用std::map和实现它们:
bool operator==(const Person &person1, const Person &person2) {
return person1.name == person2.name || person1.id == person2.id;
}
bool operator<(const Person &person1, const Person &person2) {
return person1.id < person2.id;
}
Run Code Online (Sandbox Code Playgroud)
我||在重载==运算符中使用了逻辑“或”( ),以实现可以通过人名或ID 找到电话号码的功能,而不是同时需要两者。
我编写了一些代码来测试该功能:
// Add some entries …Run Code Online (Sandbox Code Playgroud) std::map 是否将元素存储为std::pair?迭代地图看起来像这样:
#include <map>
int main() {
std::map<int, char> m;
m[1] = 'A';
m[2] = 'B';
m[3] = 'C';
std::map<int, char>::iterator it;
for(it = m.begin(); it != m.end(); it++)
{
std::cout << "Key: " << it->first;
std::cout << " Value: " << it->second;
std::cout << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud) 为什么 c++ 标准映射非常有名,而成对数组也与那些相似?
什么时候使用 C++ 标准映射更好,什么时候使用成对数组?或者两者的应用程序相似?
std::inserter 遇到的问题是,如果键已经存在,它会调用 map 的 insert 为 noop。有std::map::insert_or_assign,但我找不到使用它的插入器。
C ++ 20中有这样的东西吗?
注意:我知道我可以从 SO/互联网上的某个地方 c/p 它,我对 STL/boost 解决方案感兴趣,而不是从某个地方的 c/p 实现。
这段代码
#include <iostream>
#include <map>
int main()
{
std::map<int, std::size_t> m;
m[0] = m.size();
std::cout << m[0] << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
1。vc++ 的结局如何0?实际应用中,有什么情况std::unordered_map必须用 来代替std::map?
我知道它们之间的区别,比如内部实现、搜索元素的时间复杂度等等。
但我实在找不到确实std::unordered_map可以替代的情况std::map。
下面的代码在声明迭代器的行生成语法错误:
template <typename T>
class A
{
public:
struct B
{
int x, y, z;
};
void a()
{
std::map<int, B>::const_iterator itr; // error: ; expected before itr
}
std::vector<T> v;
std::map<int, B> m;
};
Run Code Online (Sandbox Code Playgroud)
这只有在A是模板化类时才会发生.这段代码出了什么问题?如果我将B移出A,代码编译得很好.
MSVC++"实用程序"标头中make_pair的定义是:
template<class _Ty1,
class _Ty2> inline
pair<_Ty1, _Ty2> make_pair(_Ty1 _Val1, _Ty2 _Val2)
{ // return pair composed from arguments
return (pair<_Ty1, _Ty2>(_Val1, _Val2));
}
我一直使用make_pair,但不将参数类型放在尖括号中:
map<string,int> theMap ;
theMap.insert( make_pair( "string", 5 ) ) ;
我不应该告诉make_pair第一个论点是std::string不是char*?
怎么知道的?