This question already has an answer here:
invalid operator < while sorting std::list
(1 answer)
Closed 7 years ago.
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
bool fun(int i, int j){
return abs(i - j) != -1;
}
int main(){
vector <int> v = { 1, 2, 3, 4, 5 };
sort(v.begin(), v.end(), fun);
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
return 0;
}
When I use the comparator 'fun' the programs throws the exception 'invalid operator <'. How can I modify this function to make the program run normally?
Your fun function does not provide strict weak ordering. If i and j are equal, it will return true. So you are not following the rules. Your implementation of the standard library responds by throwing an exception.
How can I modify this function to make the program run normally?
Assuming you want to sort in ascending order. Just use operator <.
bool fun(int i, int j)
{
return i < j;
}
Alternatively you could just use the comparator provided by the standard.
sort(v.begin(), v.end(), std::less<int>());
See the link in the comments or the answer provided by #DanielDaranas to understand why your original function doesn't work.
Related
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstring>
using namespace std;
int main() {
const int size = 15;
char array1[size];
char array2[size] = "four";
cout << array2[3]='\0' << endl;
return 0;
}
Hello, I am new to C++, I am learning the C-string, my question is why
array2[3] ='\0' can not be use inside the cout, the error I get is:
reference to overloaded function could not be resolved.
It works fine if it's outside of cout. thank you :)
I am trying to use boost::container::map. During inserting data, the error "insert is ambiguous" is shown.
#include <boost/container/map.hpp>
#include <string>
#include <iostream>
int main()
{
boost::container::map<std::string, int> map;
map.insert("Ram",0);
}
Your style of inserting is not correct. I provide the code:
#include <boost/container/map.hpp>
#include <string>
#include <iostream>
#include <ostream>
int main()
{
boost::container::map<std::string, int> map;
map.insert(std::pair<const std::string, int>("Ram",1));
std::cout<< map["Ram"];
return 0;
}
The following gives an error for the code mentioned below.
Where I have gone wrong ?
error: ‘function’ is not a member of ‘std’
I want to make priority queue using C++ std lib queue, and min the queue is that IceCream which takes least time to prep. I have tried implementing this -> declaring a priority_queue in c++ with a custom comparator
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <iterator>
#include <string>
#include <sstream>
using namespace std;
class IceCream
{
public:
int time_to_prep;
IceCream(int flav) {
time_to_prep = flav;
}
};
bool Compare(IceCream a, IceCream b)
{
return a.time_to_prep > b.time_to_prep;
}
int main()
{
priority_queue<IceCream, vector<IceCream>, std::function<bool(IceCream a, IceCream b)> > my_pq(Compare);
my_pq.push(IceCream(4));
my_pq.push(IceCream(33));
my_pq.push(IceCream(9));
cout << my_pq.top() << endl;
return 0;
}
#include <functional>
You need this include to get access to std::function
See: http://en.cppreference.com/w/cpp/utility/functional/function
I am creating a small C++ program for homework. I im trying to populate a 2D vector but when I write matriz[iA][iB]=iNum; it gives me the error "no match for 'operator='"
#include <iostream>
#include <algorithm>
#include <math.h>
#include <fstream>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <iomanip>
#include <set>
#include <vector>
#include <map>
using namespace std;
void popularMatriz(int iTamano, vector<vector<int>> *matriz){
for(int iA=0; iA<iTamano; iA++){
for(int iB=0; iB>iTamano; iB++){
int iNum;
scanf("%d", &iNum );
matriz[iA][iB]=iNum;
}
}
}
int main(){
int iTamano;
scanf("%d", &iTamano);
vector<vector<int>> matriz(iTamano, vector<int>(iTamano));
matriz[2][2]=5;
popularMatriz(iTamano, &matriz);
return 0;
}
You're passing a pointer to matriz; so is wrong use it as
matriz[iA][iB]=iNum;
I suggest you to pass it as reference; I mean, define popularMatriz() as
void popularMatriz(int iTamano, vector<vector<int>> & matriz)
and call it without &
popularMatriz(iTamano, matriz);
You're taking a pointer to matriz. Do this instead:
(*matriz)[iA][iB]=iNum;
I am currently trying to write a list of pairs. my code is :
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <list>
using namespace std;
list<pair<string,char>> listPair;
list<pair<string,char>>::iterator it;
void printStars(list<pair<string,char>> listPair)
{
for (it=listPair.begin(); it != listPair.end(); it++)
cout << it->first <<" ";
cout << endl;
}
int main()
{
pair<string,char> mypair;
listPair.push_back(make_pair("bib",'a'));
listPair.push_back(make_pair("bob",'b'));
for_each(listPair.begin(), listPair.end(), printStars);
return 0;
}
Compilation fails with:
error C2664: 'void (std::list<_Ty>)' : cannot convert parameter 1 from 'std::pair<_Ty1,_Ty2>' to 'std::list<_Ty>'
Can you please help me detect where exactly is the problem?
The functor you pass to std::for_each is expected to accept an element of the range you pass into std::for_each. Your last has pair<string,char> elements, so your functor should have a signature like: void printStars(const pair<string,char>& elem).
In addition, to pass a plain function to std::for_each you need to use std::ref or (on an old compiler) std::ptr_fun.
#include <iostream>
#include <algorithm>
#include <list>
#include <string> // missing include
#include <utility>
#include <functional>
using namespace std;
typedef list< pair<string,char> > list_t;
list_t listPair;
void printStars(list_t::reference x) // use a reference, otherwise you create a copy
{
cout << x.first << " " << x.second << endl;
}
int main()
{
pair<string,char> mypair;
listPair.push_back(make_pair("bib",'a'));
listPair.push_back(make_pair("bob",'b'));
for_each(listPair.begin(), listPair.end(), std::ref(printStars)); // C++11
for_each(listPair.begin(), listPair.end(), std::ptr_fun(&printStars)); // C++98
return 0;
}
Your problem is, your printStars() expects a list, however for_each passes it each item, not the actual list:
Working code :
#include <iostream>
#include <algorithm>
#include <iterator>
#include <list>
#include <string>
#include <utility>
list<pair<string,char> > listPair;
list<pair<string,char> >::iterator it;
void printStars(const pair<string,char> & listPair){ //notice the &, so it would pass by reference and not make a new copy of the pair.
cout << listPair.first << ' ';
}
int main() {
pair<string,char> mypair;
listPair.push_back(make_pair("bib",'a'));
listPair.push_back(make_pair("bob",'b'));
for_each(listPair.begin(), listPair.end(), printStars);
cout << endl;
return 0;
}