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
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,
*++isi, *++isi
will eval the first, or the second, pre increment before the other. See this article for more info. – Esquiremake_pair(*++isi,*++isi)
, you should never define names like_M_N
or_M_Values
; you are probably mimicking some STL code that you have seen, but identifiers that begin with and underscore followed by a capital letter are precisely reserved for the implementation (see https://mcmap.net/q/20769/-what-are-the-rules-about-using-an-underscore-in-a-c-identifier/688659 for other reserved kinds) (by the way, STL implementations usually use_M_xxx
for class member variables, not local variables). – Workmanship