预增量不能按我预期的那样工作 [英] pre-increment not working as i expect
问题描述
我正在尝试通过在线解决一些问题来学习动态编程.我遇到的一个问题需要处理以下输入
I am trying to learn dynamic programming by solving some questions online. One question that i came across requires the to process the following input
4 10 3 4 4 5 6 7 5 7
4 10 3 4 4 5 6 7 5 7
第一个指向项目数量,第二个指向总容量,其余四个(成对)现在应该指向值和容量.
The first points at number of items, second the total capacity and the rest of the four (in a pair) should now point at the value and capacity.
我遇到的问题是解析它的代码.
The problem i have is with the code which parses it.
#include<iostream>
#include<map>
#include<iterator>
using namespace std;
template<typename T>
void print(typename T::const_iterator start,
typename T::const_iterator end)
{
typename T::const_iterator itr=start;
while(itr!=end)
{
std::cout << itr->first << " " << itr->second << std::endl;
itr++;
}
}
int main()
{
int _M_N; // total numbers
int _M_V; // total values
std::map<int,int> _M_Values;
istream_iterator<int> isi(cin);
_M_N = *isi;
_M_V = *++isi;
for(int i=0;i<_M_N;i++)
{
//int m=*++isi,n=*++isi;
//_M_Values.insert(make_pair(m,n));
_M_Values.insert(make_pair(*++isi,*++isi));
}
print<std::map<int,int> >(_M_Values.begin(),_M_Values.end());
}
当我运行此程序时,此输入得到错误的输出
when i run this program i get the wrong output for this input
akhetarp@desktop> knapsack < test
4 3
5 4
7 6
但是,如果我取消注释该行并注释前面的插入行,则会得到预期的结果
if i however uncomment the line and comment the earlier line for insertion, i get the expected
int m=*++isi,n=*++isi;
_M_Values.insert(make_pair(m,n));
// _M_Values.insert(make_pair(*++isi,*++isi));
3 4
4 5
6 7
5 7
我知道这一定是一些基本错误. 预先感谢,
I know it has to be some basic error. Thanks in advance,
推荐答案
您在此处缺少序列点:
_M_Values.insert(make_pair(*++isi,*++isi));
声明中的逗号为1,函数中的逗号不是:
The comma in a declaration is one, the comma in a function not:
int m=*++isi,n=*++isi;
请查看: http://en.wikipedia.org/wiki/Sequence_point
这篇关于预增量不能按我预期的那样工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!