对的矢量-逗号操作数的左手边没有影响

Vector of pairs - left hand side of operand of comma has no effect

本文关键字:有影响 操作数      更新时间:2023-10-16

我声明了一个向量a与配对

vector <pair <int, int> > args;

然后我想把一对像这样推到向量中:

args.push_back((1,-1));

它告诉我逗号的左边操作数没有效果。我哪里错了?

说出args.push_back(std::make_pair(1,-1));。或者任何数量的替代品:

// #1
args.push_back(std::pair<int, int>(1,-1));
// #2
typedef std::vector< std::pair<int, int> > pvector;
pvector args;
args.push_back(pvector::value_type(1,-1));
// #3
typedef std::pair<int, int> intpair;
std::vector<intpair> args;
args.push_back(intpair(1,-1));
// #4
args.emplace_back(1, -1);  // sexy
//...

(1,-1(是一种语法,意思是"计算1,计算-1,然后使用-1作为值"。这与制作这对情侣的实例无关。你必须使用std::make_pair(1,-1)来制作你推的那对。