【发布时间】:2021-10-21 12:55:07
【问题描述】:
我在第 15 行和第 20 行遇到问题,我试图将元素推回对向量中
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
int main()
{
int x, y, a, b, c, d, j, m, v;
vector<pair<int, int> > ladder;
cin >> x >> y;
for (int i = 0; i < x; i++) {
cin >> a >> b;
ladder.push_back(pair(a, b));
}
vector<pair<int, int> > snake;
for (int i = 0; i < y; i++) {
cin >> c >> d;
snake.push_back(pair(c, d));
}
vector<int> moves;
cin >> v;
while (v != 0) {
moves.push_back(v);
v = 0;
cin >> v;
}
return 0;
}
我的错误是:
prog.cpp: In function ‘int main()’:
prog.cpp:15:30: error: missing template arguments before ‘(’ token
ladder.push_back(pair(a, b));
^
prog.cpp:20:29: error: missing template arguments before ‘(’ token
snake.push_back(pair(c, d));
我这里有代码要测试: https://ideone.com/ZPKP4s
【问题讨论】:
-
第 15 行是
vector<int> moves;,第 20 行是cin >> v;- 如果我计算正确 - 最好在相关行后面添加评论,并将确切的错误消息作为文本发布。 -
另外,您正在使用哪种编译器以及您正在使用哪些命令行选项,尤其是哪种语言标准(CTAD 是 C++17 的新标准)。
-
使用 C++17 编译器。
-
@RaymondChen 我正在使用 c++14,所以这可能是个问题。但我在评论中使用 scohoe001s 方法解决了这个问题。也许旧版本需要指定,而新版本不需要
-
C++14 要求您指定类型。 C++17 添加了一个名为 CTAD 的功能,可以让您省略它。 C++14 确实有一个简化的对构造机制:
ladder.push_back(std::make_pair(a, b))。或者您可以使用ladder.emplace_back(a, b)并让emplace进行配对。
标签: c++ data-structures std-pair