Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在另一个std::pair中插入一对std::pair?_C++_Templates_Stl - Fatal编程技术网

C++ 如何在另一个std::pair中插入一对std::pair?

C++ 如何在另一个std::pair中插入一对std::pair?,c++,templates,stl,C++,Templates,Stl,我声明一个字符串到一对对的映射,如下所示: std::map<std::wstring, std::pair<std::pair<long, long>, std::pair<long, long>>> reference; std::地图参考; 我将其初始化为: reference.insert(L"First", std::pair<std

我声明一个字符串到一对对的映射,如下所示:

std::map<std::wstring, 
         std::pair<std::pair<long, long>, 
                   std::pair<long, long>>> reference;
std::地图参考;
我将其初始化为:

reference.insert(L"First", 
                 std::pair<std::pair<long, long>, 
                           std::pair<long, long>>(std::pair<long, long>(-1, -1),
                           std::pair<long, long>(0, 0)));
reference.插入(L“First”,
std::pair(std::pair(-1,-1),
std:pair(0,0));

但是,VisualC++给出了错误“C2664,没有构造函数可以接受源类型,或者构造函数重载解析是不明确的”。


我不熟悉使用模板和STL,我不知道我做错了什么。

无法正确解析
>
(除非您有C++0x编译器)

更改为
>>

这:

应该是:

reference.insert(L"First",
                ^^^
此外,还有一个实用功能,使成对的构造更容易:

std::pair<std::pair<long, long>, std::pair<long, long>>(std::pair<long, long>(-1, -1), std::pair<long, long>(0, 0))
试试这个:

reference[L"First"]
    = std::make_pair(std::make_pair(-1L,-1L),std::make_pair(0L,0L));

关闭模板时,C++会被连续的“>”弄糊涂,因为它将其解释为移位运算符


在结束模板之间添加空格,将>>>更改为>>
map::insert
本身接受一个
std::pair
参数,而不是两个参数。您可以通过使用
std::make_pair
(从函数参数推断模板参数)整理代码,以获得如下结果:

reference.insert(std::make_pair("First", 
                                std::make_pair(std::make_pair(-1L,-1L),
                                               std::make_pair(0L,0L))));

在调试这类事情时,使用typedefs是有帮助的

// test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <map>
#include <string>

int _tmain(int argc, _TCHAR* argv[])
{
    typedef std::pair<long, long> ElementType;
    typedef std::pair<ElementType, ElementType> ValueType;
    typedef std::wstring KeyType;
    std::map<KeyType, ValueType> reference;

    KeyType key = L"First";
    reference[key] = ValueType(ElementType(-1, -1), ElementType(0, 0));

    return 0;
}
//test1.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括
#包括
int _tmain(int argc,_TCHAR*argv[]
{
typedef std::pair ElementType;
typedef std::pair ValueType;
typedef std::wstring keype;
地图参考;
KeyType key=L“第一”;
reference[key]=ValueType(ElementType(-1,-1),ElementType(0,0));
返回0;
}

您可以通过创建一个helper函数来简化代码,以创建成对的对,类似于标准库中可用的
std::make_pair
helper函数。另外,使用映射
运算符[]
进行插入,可使代码更易于阅读:

template<typename T, typename U, typename V, typename W>
std::pair< std::pair<T,U>, std::pair<V,W> > make_pair_pair(T t, U u, V v, W w) {
   // using std::make_pair instead of the constructor for better readability
   return std::make_pair(std::make_pair(t, u), std::make_pair(v, w));
}

reference[L"First"] = make_pair_pair(1,2,3,4);
模板
std::pairmake_pair_pair_pair(T,U,V,W){
//使用std::make_pair代替构造函数以提高可读性
返回std::make_pair(std::make_pair(t,u),std::make_pair(v,w));
}
参考文献[L“First”]=形成对(1,2,3,4);

请使用typedef和
std::make_pair
使其易读。我重新设置了格式,使其更容易看。您不能切换到使用
std::tr1::tuple
而不是嵌套所有这些
std::pair
吗?Boost也有一个元组实现。我听说你们喜欢std::pair,所以我在你们的……中放了一个std::pair,因为我知道tuple并不是所有供应商都实现的。在我转到Boost之前,我正在尝试先学习STL。谢谢你的帮助。我编辑了我的问题,将L“”添加到我的wstring中,以使问题更易于“解析”。这是一个比我更好的答案,但请注意,使用
reference[key]=value
插入可以为
reference.insert(make_pair(key,value))
提供不同的行为;使用
[]
将覆盖现有元素,而
insert
将不会覆盖。OP尝试使用的
insert()
函数并不存在,即使您
\include
。您发布的示例代码工作正常,可读性很好,但因为您替换了
insert()
,而不是像您所说的那样,因为包含…如果您删除了“you just need
”部分,我认为这有点误导,我会认为它很有帮助,并会对它进行投票。好的,您明白了。我不知道您是否意识到您在回答中写了“运算符[]以使插入结果更易于阅读代码”。正如您所说,insert不存在!是的,我意识到了这一点,我在发布我的答案后看到了你的答案,我想“谁否决了这一点,接线员[]才是正确的选择!”。然后,我被
的东西弄糊涂了,于是我开始尝试,如果
wstring(const char*)
的构造函数真的丢失了,或者这样会产生问题中的错误消息。不过,我无法复制类似的内容,所以我决定发表评论。(+1)
// test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <map>
#include <string>

int _tmain(int argc, _TCHAR* argv[])
{
    typedef std::pair<long, long> ElementType;
    typedef std::pair<ElementType, ElementType> ValueType;
    typedef std::wstring KeyType;
    std::map<KeyType, ValueType> reference;

    KeyType key = L"First";
    reference[key] = ValueType(ElementType(-1, -1), ElementType(0, 0));

    return 0;
}
template<typename T, typename U, typename V, typename W>
std::pair< std::pair<T,U>, std::pair<V,W> > make_pair_pair(T t, U u, V v, W w) {
   // using std::make_pair instead of the constructor for better readability
   return std::make_pair(std::make_pair(t, u), std::make_pair(v, w));
}

reference[L"First"] = make_pair_pair(1,2,3,4);