Usage of multimap with next_permutation c++ - c++

i am just trying to implement Knapsack Problem in a Naive way to stress test my original solution.
MY CODE
double StressTest(multimap<int, int> ValWt, int KnapscakWeight)
{
vector<double> TotalValue;
double Temp_KnapsackWeight = 0.0;
double Value = 0.0;
multimap<int, int>::iterator itr1;// = ValWt.begin();
do
{
itr1 = ValWt.begin();
Temp_KnapsackWeight = KnapscakWeight;
while( (Temp_KnapsackWeight > 0) && (itr1 != ValWt.end()) )
{
if(itr1->second > Temp_KnapsackWeight)
{
Value += ( (Temp_KnapsackWeight/itr1->second) * itr1->first );
Temp_KnapsackWeight = 0;
}
else
{
Temp_KnapsackWeight -= itr1->second;
Value += itr1->first;
}
itr1++;
}
TotalValue.push_back(Value);
Value = 0.0;
}while( next_permutation(ValWt.begin(), ValWt.end()) );
return *max_element(TotalValue.begin(), TotalValue.end());
}
ERROR
In file included from /usr/include/c++/7/bits/char_traits.h:39:0,
from /usr/include/c++/7/ios:40,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from 2_max_val_of_loot.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h: In instantiation of ‘void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = std::_Rb_tree_iterator<std::pair<const int, int> >; _ForwardIterator2 = std::_Rb_tree_iterator<std::pair<const int, int> >]’:
/usr/include/c++/7/bits/stl_algo.h:2926:22: required from ‘bool std::__next_permutation(_BidirectionalIterator, _BidirectionalIterator, _Compare) [with _BidirectionalIterator = std::_Rb_tree_iterator<std::pair<const int, int> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]’
/usr/include/c++/7/bits/stl_algo.h:2966:2: required from ‘bool std::next_permutation(_BIter, _BIter) [with _BIter = std::_Rb_tree_iterator<std::pair<const int, int> >]’
2_max_val_of_loot.cpp:39:53: required from here
/usr/include/c++/7/bits/stl_algobase.h:148:11: error: use of deleted function ‘typename std::enable_if<(! std::__and_<std::__is_swappable<_T1>, std::__is_swappable<_T2> >::value)>::type std::swap(std::pair<_T1, _T2>&, std::pair<_T1, _T2>&) [with _T1 = const int; _T2 = int; typename std::enable_if<(! std::__and_<std::__is_swappable<_T1>, std::__is_swappable<_T2> >::value)>::type = void]’
swap(*__a, *__b);
~~~~^~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/stl_algobase.h:64:0,
from /usr/include/c++/7/bits/char_traits.h:39,
from /usr/include/c++/7/ios:40,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from 2_max_val_of_loot.cpp:1:
/usr/include/c++/7/bits/stl_pair.h:503:5: note: declared here
swap(pair<_T1, _T2>&, pair<_T1, _T2>&) = delete;
MY OBSERVATION
next_permutation() is creating error, but why i don't understand why.
next_permutation() requires bidirectional iterator and multimap iterator is a bidirectional iterator.
I doubt as multimap are sorted always, that's why the error is shown ??
Please Help.
Thanking You.

You cannot use std::map or std::multimap (or unordered versions) as std::next_permutation requires:
-BidirIt must meet the requirements of ValueSwappable and LegacyBidirectionalIterator.
but std::multimap values are not swappable as key in the map is not mutable:
value_type std::pair<const Key, T>
(emphasis is mine)
Less formally order of elements in map is determined and cannot me changed. You have to use different container like std::vector to perform this operation.

Related

Using std::swap_ranges with std::map

I would like to swap parts of two maps using a standard algorithm, but somehow iterators on map do not seem to be swappable. I am surely missing something.
Example
#include<map>
#include<algorithm>
auto function(std::map<int,int> m1, std::map<int,int> m2)
{
auto first = m1.begin();
auto last = first;
std::advance(last, 3);
std::swap_ranges(first, last, m2.begin());
}
Error
See it on compiler explorer: https://godbolt.org/z/6bn1xYTTr
In file included from /opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/stl_tree.h:63,
from /opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/map:60,
from <source>:1:
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/stl_algobase.h: In instantiation of 'void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = _Rb_tree_iterator<pair<const int, int> >; _ForwardIterator2 = _Rb_tree_iterator<pair<const int, int> >]':
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/stl_algobase.h:212:16: required from '_ForwardIterator2 std::swap_ranges(_ForwardIterator1, _ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = _Rb_tree_iterator<pair<const int, int> >; _ForwardIterator2 = _Rb_tree_iterator<pair<const int, int> >]'
<source>:9:21: required from here
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/stl_algobase.h:182:11: error: use of deleted function 'typename std::enable_if<(! std::__and_<std::__is_swappable<_T1>, std::__is_swappable<_T2> >::value)>::type std::swap(pair<_T1, _T2>&, pair<_T1, _T2>&) [with _T1 = const int; _T2 = int; typename enable_if<(! __and_<__is_swappable<_T1>, __is_swappable<_T2> >::value)>::type = void]'
182 | swap(*__a, *__b);
| ~~~~^~~~~~~~~~~~
In file included from /opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/stl_algobase.h:64:
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/stl_pair.h:715:5: note: declared here
715 | swap(pair<_T1, _T2>&, pair<_T1, _T2>&) = delete;
| ^~~~
Compiler returned: 1
std::map is implemented as some sort of binary search tree, and its structure depends on each item's key never changing. Thus the type returned by *m1.begin() is std::pair<const int, int>&. Note that the first int is const. You cannot modify it.
std::swap_ranges tries to swap each element of the first range with its corresponding element in the second range, but std::pair<const int, int>s can't be swapped due to the constness of their first element.

How can I use predicate function in find_if algorithm?

The predicate function:
bool Schedule::predicateFunc(map<pair<string,int>,pair<string,Array> >::iterator it,string &a)
{
return (it->first).first == a;
}
Function which I have to use predicate func:
void Schedule::studentSchedule() {
string s,c;
cout<<"Enter the student and course name to create schedule"<<endl;
cin>>s>>c;
list<string>::iterator studentLoc;
map<pair<string,int>,pair<string,Array> >::iterator courseL;
map<pair<string,int>,pair<string,Array> >::iterator location;
studentLoc = find(getStudentList().begin(),getStudentList().end(),s);
location = find_if(getMatchMap().begin(), getMatchMap().end(), predicateFunc(courseL,c) )
if(studentLoc != getStudentList().end() && location != getMatchMap().end())
{
cout<<"I found it"<<endl;
}
else
cout<<"I cant found it"<<endl;
}
When I tried to use predicate function in here:
location = find_if(getMatchMap().begin(), getMatchMap().end(), predicateFunc(courseL,c) )
I am getting an error like this:
In file included from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algobase.h:71,
from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/algorithm:61,
from C:\Users\Fatih\Desktop\clion\SchoolProject1\Schedule.cpp:4:
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_pred<_Predicate>::operator()(_Iterator) [with _Iterator = std::_Rb_tree_iterator<std::pair<const std::pair<std::__cxx11::basic_string<char>, int>, std::pair<std::__cxx11::basic_string<char>, std::array<int, 6> > > >; _Predicate = bool]':
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:104:42: required from '_InputIterator std::__find_if(_InputIterator, _InputIterator, _Predicate, std::input_iterator_tag) [with _InputIterator = std::_Rb_tree_iterator<std::pair<const std::pair<std::__cxx11::basic_string<char>, int>, std::pair<std::__cxx11::basic_string<char>, std::array<int, 6> > > >; _Predicate = __gnu_cxx::__ops::_Iter_pred<bool>]'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:161:23: required from '_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = std::_Rb_tree_iterator<std::pair<const std::pair<std::__cxx11::basic_string<char>, int>, std::pair<std::__cxx11::basic_string<char>, std::array<int, 6> > > >; _Predicate = __gnu_cxx::__ops::_Iter_pred<bool>]'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:3930:28: required from '_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = std::_Rb_tree_iterator<std::pair<const std::pair<std::__cxx11::basic_string<char>, int>, std::pair<std::__cxx11::basic_string<char>, std::array<int, 6> > > >; _Predicate = bool]'
C:\Users\Fatih\Desktop\clion\SchoolProject1\Schedule.cpp:25:93: required from here
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/predefined_ops.h:283:11: error: expression cannot be used as a function
{ return bool(_M_pred(*__it)); }
^~~~~~~~~~~~~~~~~~~~
What is the true usage of the predicate funtion in here ?
You're probably misunderstanding the concept of Predicate. It must be a function that takes one element of the collection and returns a bool. This function is now called for each element in the range until the first time it returns true (see here).
In your code, you're calling the predicate instead of passing it to find_if.
Also, the signature of your predicate is wrong: It takes two parameters instead of one. The signature should be
bool Schedule::predicateFunc(const map<pair<string,int>,pair<string,Array> >::value_type& x);
If you want to pass it an additional argument, e.g. a string to be compared with, you coulddo something like:
bool Schedule::compareName(map<pair<string,int>,pair<string,Array> >::value_type& x,string &a)
{
return (x.first).first == a;
}
and then in the calling code:
std::string expected_name = "some name";
auto predicate = [&](auto& course) { return compareName(course, expected_name); };
location = find_if(getMatchMap().begin(), getMatchMap().end(), predicate);
You are calling your predicate function but you must provide a reference to your predicate function:
location = find_if(getMatchMap().begin(), getMatchMap().end(), predicateFunc);
Also, the signature of your predicate function is not correct. It should take only one argument and this argument is not an iterator, but the value of the collection/iterator. It is probably also a good idea to make it a const-reference.
bool Schedule::predicateFunc(const map<pair<string,int>,pair<string,Array> >::value_type& x);
If you need to provide an argument to your predicate function, you have several options:
Do not use a separate predicate function, but a lambda expression.
Use std::bind().
Use a function object.

Trying to use find_if function to locate value in vector of pairs by first element

Have piece of code to scan large string (html) and then parse any words out of it. Push all the instances to vector or pairs (word and count) in case it's not there already, otherwise increment count (second element of pair).
vector < pair <string,int> > vect;
vector < pair <string,int> >::iterator it;
...
it = find_if (vect.begin(), vect.end(), currentword);
if (it != vect.end())
it->second++;
else
vect.push_back( make_pair(currentword, 1));
...
Getting compilation error:
In file included from C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/bits/stl_algobase.h:71:0,
from C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/bits/char_traits.h:39,
from C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/ios:40,
from C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/ostream:38,
from C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/iostream:39,
from webCounter.cpp:19:
C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_pred::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator, int>*, std::vector, int> > >; _Predicate = std::__cxx11::basic_string]':
C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/bits/stl_algo.h:120:14: required from '_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator, int>*, std::vector, int> > >; _Predicate = __gnu_cxx::__ops::_Iter_pred >]'
C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/bits/stl_algo.h:161:23: required from '_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = __gnu_cxx::__normal_iterator, int>*, std::vector, int> > >; _Predicate = __gnu_cxx::__ops::_Iter_pred >]'
C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/bits/stl_algo.h:3815:28: required from '_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = __gnu_cxx::__normal_iterator, int>*, std::vector, int> > >; _Predicate = std::__cxx11::basic_string]'
webCounter.cpp:68:58: required from here
C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include/c++/bits/predefined_ops.h:234:30: error: no match for call to '(std::__cxx11::basic_string) (std::pair, int>&)'
{ return bool(_M_pred(*__it)); }
The problem is there is no built-in comparison between a pair <string,int> and what appears to be a string. You will have to provide one. Eg.
it = find_if (vect.begin(),
vect.end(),
[currentword](const pair <string,int>& p){ // this is a lambda expression
return p.first == currentword; // compare strings
});
Documentation for Lambda Expressions
That said,
std::map<string, int> freqmap;
could be a better choice for this task if it is allowed. Just about all of the code reduces to
freqmap[currentword]++;
Documentation for std::map

Erase element of struct vector

I am trying to delete a Team Roster element from the playerRoster vector given a user defined jersey number. The problem I am running into is the .erase function isn't working.
struct TeamRoster {
int jerseyNumber;
int playerRating;
};
int numPlayers = 5;
int searchValue = 0;
vector<TeamRoster> playerRoster(numPlayers);
TeamRoster newPlayer;
unsigned int i = 0;
cout << "Enter a jersey number: ";
cin >> searchValue;
cout << endl;
for (i = 0; i < playerRoster.size(); ++i) {
if (playerRoster.at(i).jerseyNumber == searchValue) {
playerRoster.erase(i);
}
}
I am getting this error.
main.cpp: In function 'int main()':
main.cpp:68:36: error: no matching function for call to 'std::vector<main()::TeamRoster>::erase(unsigned int&)'
playerRoster.erase(i);
^
In file included from /usr/include/c++/6/vector:64:0,
from main.cpp:2:
/usr/include/c++/6/bits/stl_vector.h:1147:7: note: candidate: std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::const_iterator) [with _Tp = main()::TeamRoster; _Alloc = std::allocator<main()::TeamRoster>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<main()::TeamRoster*, std::vector<main()::TeamRoster> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = main()::TeamRoster*; std::vector<_Tp, _Alloc>::const_iterator = __gnu_cxx::__normal_iterator<const main()::TeamRoster*, std::vector<main()::TeamRoster> >; typename __gnu_cxx::__alloc_traits<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type>::const_pointer = const main()::TeamRoster*]
erase(const_iterator __position)
^~~~~
/usr/include/c++/6/bits/stl_vector.h:1147:7: note: no known conversion for argument 1 from 'unsigned int' to 'std::vector<main()::TeamRoster>::const_iterator {aka __gnu_cxx::__normal_iterator<const main()::TeamRoster*, std::vector<main()::TeamRoster> >}'
/usr/include/c++/6/bits/stl_vector.h:1174:7: note: candidate: std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::const_iterator, std::vector<_Tp, _Alloc>::const_iterator) [with _Tp = main()::TeamRoster; _Alloc = std::allocator<main()::TeamRoster>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<main()::TeamRoster*, std::vector<main()::TeamRoster> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = main()::TeamRoster*; std::vector<_Tp, _Alloc>::const_iterator = __gnu_cxx::__normal_iterator<const main()::TeamRoster*, std::vector<main()::TeamRoster> >; typename __gnu_cxx::__alloc_traits<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type>::const_pointer = const main()::TeamRoster*]
erase(const_iterator __first, const_iterator __last)
^~~~~
/usr/include/c++/6/bits/stl_vector.h:1174:7: note: candidate expects 2 arguments, 1 provided
I looked on cplusplus.com and noticed a difference between c++94 and c++11 iterator erase (const_iterator position); (11), iterator erase (iterator position); (94). From the error I'm getting, it looks like my compiler is using c++11.
Does const_iterator mean that I have to use a constant with the .erase() function, and if so, is there a different function that will work in this case?
If this isn't the problem, can you help me figure out what is?
If only one element you want to erase, you could try code below:
for(auto iter = playerRoster.begin(); iter != playerRoster.end(); ++iter){
if(iter->jerseyNumber == searchValue){
iter = playerRoster.erase(iter);
break;
}
}
Or if you want to erase all element that match, you could try :
for(auto iter = playerRoster.begin(); iter != playerRoster.end(); ){
if(iter->jerseyNumber == searchValue){
iter = playerRoster.erase(iter);
}else{
++iter;
}
}
Remember erase return the element after the one that be erased.
Sample code can be found : http://www.cplusplus.com/reference/vector/vector/erase/

How to pass normal param as well as template param in a template function in C++?

I have a template function (as follows) in a namespace called myNamespace:
template <typename setX>
void getRandomItems(NaturalNumber size, setX &random, setX &items)
{
assert(size <= items.size());
//set of randomly selected indices for items
set<NaturalNumber> index;
NaturalNumber r, i;
while(index.size() < size)
{
r = unifRand(0,items.size()-1);
index.insert(r);
}
typename setX::iterator it, sit = items.begin();
for(i = 0, it = index.begin(); it != index.end(); it ++)
{
//find the r-th elt in index
r = *it;
for(; i < r; i ++)
sit++;
random.insert(*sit);
}
}
However whenever I call this function I get these errors:
generic.h: In function ‘void myNamespace::getRandomItems(NaturalNumber, setX&, setX&) [with setX = std::set<std::basic_string<char> >, NaturalNumber = long unsigned int]’:
synthetic-graph.C:87:55: instantiated from here
generic.h:74:32: error: no match for ‘operator=’ in ‘it = index.std::set::begin [with _Key = long unsigned int, _Compare = std::less<long unsigned int>, _Alloc = std::allocator<long unsigned int>, std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<long unsigned int>]()’
/usr/include/c++/4.5/bits/stl_tree.h:224:5: note: candidate is: std::_Rb_tree_const_iterator<std::basic_string<char> >& std::_Rb_tree_const_iterator<std::basic_string<char> >::operator=(const std::_Rb_tree_const_iterator<std::basic_string<char> >&)
synthetic-graph.C:87:55: instantiated from here
generic.h:74:32: error: no match for ‘operator!=’ in ‘it != index.std::set<_Key, _Compare, _Alloc>::end [with _Key = long unsigned int, _Compare = std::less<long unsigned int>, _Alloc = std::allocator<long unsigned int>, std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<long unsigned int>]()’
/usr/include/c++/4.5/bits/stl_tree.h:291:7: note: candidate is: bool std::_Rb_tree_const_iterator<_Tp>::operator!=(const std::_Rb_tree_const_iterator<_Tp>::_Self&) const [with _Tp = std::basic_string<char>, std::_Rb_tree_const_iterator<_Tp>::_Self = std::_Rb_tree_const_iterator<std::basic_string<char> >]
generic.h:77:4: error: cannot convert ‘const std::basic_string<char>’ to ‘NaturalNumber’ in assignment
I have tried all combinations but no luck, please help me!!!
setX is not a set of NaturalNumbers so the iterators aren't compatible when you say it = index.begin(). You could possibly make it an iterator of set<NaturalNumber> instead, I can't quite make out what you really want to do here.
Also I noticed that in your inner loop you don't do any checks to make sure sit doesn't run off the end of its set.
You're trying to assign incompatible iterators.
Perhaps you meant
set<NaturalNumber>::iterator it;
typename setX::iterator sit = items.begin();
instead of
typename setX::iterator it, sit = items.begin();