开发者

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

I declared a vector a pairs with:

vector <pair <int, int> > a开发者_如何转开发rgs;

Then I want to push a pair into the vector like this:

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

It tells me that the left-hand operand of comma has no effect. Where do I go wrong?


Say args.push_back(std::make_pair(1,-1));. Or any number of alternatives:

// #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) is a syntax that means 'evaluate 1, evaluate -1, and then use -1 as the value'. It has nothing to do with making an instance of the pair. You have to use std::make_pair(1,-1) to make the pair that you push.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜