This question already has answers here:
What does the > operator do when comparing two C++ containers?
(1 answer)
Comparing 2 vectors using relational operators
(2 answers)
Closed 27 days ago.
I was learning on a website today and came upon this code
//code removed
vector<int> ans, path;
void dfs(int x, int y)
{
if (!x)
{
if (!ans.size() || ans>path) ans=path;
return;
}
//code removed
}
int main()
{
//code removed
}
In line 9, what does the ans>path mean? Both ans and path are vector<int> so I don't know what is being compared.
I originally thought ans>path mean ans.size()>path.size() but the code gave me a different result. I have searched online for any solutions but nothing came up. Any help is appreciated!
operator> performs lexicographical comparison. The comparison is described here:
Lexicographical comparison is an operation with the following properties:
Two ranges are compared element by element.
The first mismatching element defines which range is lexicographically less or greater than the other.
If one range is a prefix of another, the shorter range is lexicographically less than the other.
If two ranges have equivalent elements and are of the same length, then the ranges are lexicographically equal.
An empty range is lexicographically less than any non-empty range.
Two empty ranges are lexicographically equal.
Related
I have this C++ code below to solve for a homework and after I ran it with Code::Blocks, it tells me that i=0, which means the expression s.compare(t)<0 is false. But, the way I see it, it's the other way around: (s<t, because AbcA < AAbcA). Can someone please explain it to me?
#include <iostream>
#include <string>
using namespace std;
int main(void) {
string s = "Abc", t = "A";
s=s+t;
t=t+s;
int i = s.compare(t)<0;
int j = s.length()<t.length();
cout<<i+j<<endl;
return 0;
}
According to the reference std::string::compare returns:
negative value if *this appears before the character sequence specified by the arguments, in lexicographical order
zero if both character sequences compare equivalent
positive value if *this appears after the character sequence specified by the arguments, in lexicographical order
Lexicographical comparison being defined as:
Lexicographical comparison is a operation with the following properties:
Two ranges are compared element by element.
The first mismatching element defines which range is lexicographically less or greater than the other.
If one range is a prefix of another, the shorter range is lexicographically less than the other.
If two ranges have equivalent elements and are of the same length, then the ranges are lexicographically equal.
An empty range is lexicographically less than any non-empty range.
Two empty ranges are lexicographically equal.
"AbcA" comes lexicographically after "AAbc", because the first nonequal character 'b' (ASCII 0x62) comes after 'A' (ASCII 0x41)
This question already has answers here:
Does std::sort change the relative order of equal elements?
(5 answers)
Closed 4 years ago.
I'd like to reverse-sort a vector of strings by size, with the constraint that if there are 2 strings of equal length, I'd like them to retain their original order. For eg: sorting the following set of strings :-
aab
aac
aacghgh
aabghgh
should yield :-
aacghgh
aabghgh
aab
aac
Currently I'm doing the sorting as follows :-
struct comp_functor {
bool operator()(const string& s1, const string& s2) {
return s1.size() > s2.size();
}
};
struct comp_functor c;
vector<string> vecs;
sort(vecs.begin(), vecs.end(), c);
Is there any way to specify in the overloaded method that I want to retain the original ordering if they have the same length? If not, what would be the best way to use STL libraries to solve this problem?
I believe you are looking for std::stable_sort.
It preserves the order of elements which are considered equal.
If you don't want equivalent elements to change order, then don't use std::sort. Use std::stable_sort instead - it exists exactly because it has the property of not reordering equivalent elements.
This question already has answers here:
Sorting a vector of custom objects
(14 answers)
Closed 6 years ago.
I have an array of Person objects that contain a member variable, name. I am trying to order them alphabetically using an overloaded comparison operator that uses string::compare to compare the strings in the objects.
bool Person::operator==(Person p) {
if (name.compare(p.name) == 0) {
return true;
}
else {
return false;
}
}
this works and gives me the correct result but I don't know how I can use this to order the names in the array alphabetically.
I have looked around and seen that documentation on string::compare says
relation between compared string and comparing string
0 They compare equal
<0 Either the value of the first character that does not match is lower in > the compared string, or all compared characters match but the compared string > is shorter.
,>0 Either the value of the first character that does not match is greater >in the compared string, or all compared characters match but the compared string is longer.
I can't figure out a way to solve my problem with this information though.
Am I on the right track for what I'm trying to do or is there a better way?
Your operator is correctly defined, but it is not the operator you are looking for. You want to define the operator <, then use std::sort.
today I wrote this code
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector <int> a (4,100);
vector <int> b (1,100);
cout<<(b<a);
}
as the reference says this is true only if if the contents of the b are lexicographically less than the contents of a, false otherwise, but in the output I obtain true, someone can explain me.
http://en.cppreference.com/w/cpp/container/vector/operator_cmp
From http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare,
Lexicographical comparison is a operation with the following properties:
. Two ranges are compared element by element.
. The first mismatching element defines which range is lexicographically less or greater than the other.
. If one range is a prefix of another, the shorter range is lexicographically less than the other.
. If two ranges have equivalent elements and are of the same length, then the ranges are lexicographically equal.
. An empty range is lexicographically less than any non-empty range.
. Two empty ranges are lexicographically equal.
In your case, the third clause applies.
The rules for std::lexicographical_compare which is invoked to handle
a < b
say that if one range is a prefix of another, the shorter range is lexicographically less than the other.
Here the range of a refers to the elements from a.begin() up to but excluding a.end() - in other words all the elements in a
Similarly, the range of b refers to the elements from b.begin() up to but excluding b.end() - in other words all the elements in b
b (which holds a single int of value 100) IS a prefix of a (which holds 4 integers of value 100) so b (the shorter range) is considered less than a, hence b < a is true.
See http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare for further details.
This question already has answers here:
Standard library sort and user defined types
(3 answers)
Closed 8 years ago.
I have a two dimensional vector A
vector < vector <int> > A;
the inner vector is of constant length 3. I want to sort A according to the values of 2nd element in the inner vector. Like we can sort a database table according to any of its column.
How do I do that?
std::sort(std::begin(m), std::end(m),
[](std::vector<int> const &lhs,
std::vector<int> const &rhs) { return lhs[1] < rhs[1]; });
Be careful though as this is not a stable sort and will not preserve the order of lines that have the same second element. Use std::stable_sort if you want this. The usage is identical.