lexicographic

What's the simplest way of defining lexicographic comparison for elements of a class?

一笑奈何 提交于 2019-11-28 08:29:45
If I have a class that I want to be able to sort (ie support a less-than concept), and it has several data items such that I need to do lexicographic ordering then I need something like this: struct MyData { string surname; string forename; bool operator<(const MyData& other) const { return surname < other.surname || (surname==other.surname && forename < other.forename); } }; This becomes pretty unmanageable for anything with more than 2 data members. Are there any simpler ways of achieving it? The data members may be any Comparable class. tuple is a good idea, but if you want to keep having

Sort list of strings ignoring upper/lower case

不羁岁月 提交于 2019-11-28 06:24:25
I have a list which contains strings representing animal names. I need to sort the list. If I use sorted(list) , it will give the list output with uppercase strings first and then lowercase. But I need the below output. Input: var = ['ant','bat','cat','Bat','Lion','Goat','Cat','Ant'] Output: ['ant', 'Ant', 'bat', 'Bat', 'cat', 'Cat', 'Goat', 'Lion'] The sort() method and the sorted() function take a key argument: var.sort(key=lambda v: v.upper()) The function named in key is called for each value and the return value is used when sorting, without affecting the actual values: >>> var=['ant',

What does lexical file name order mean?

余生颓废 提交于 2019-11-28 01:46:29
问题 In the package initialization part of the Go specification, what does "lexical file name order" mean? To ensure reproducible initialization behavior, build systems are encouraged to present multiple files belonging to the same package in lexical file name order to a compiler. 回答1: From Wikipedia: Lexical order is a generalization of the way the alphabetical order of words is based on the alphabetical order of their component letters. In practice this means files names are compared as strings,

Sort list of strings ignoring upper/lower case

社会主义新天地 提交于 2019-11-27 20:31:26
问题 I have a list which contains strings representing animal names. I need to sort the list. If I use sorted(list) , it will give the list output with uppercase strings first and then lowercase. But I need the below output. Input: var = ['ant','bat','cat','Bat','Lion','Goat','Cat','Ant'] Output: ['ant', 'Ant', 'bat', 'Bat', 'cat', 'Cat', 'Goat', 'Lion'] 回答1: The sort() method and the sorted() function take a key argument: var.sort(key=lambda v: v.upper()) The function named in key is called for

What's the simplest way of defining lexicographic comparison for elements of a class?

谁说我不能喝 提交于 2019-11-27 02:15:50
问题 If I have a class that I want to be able to sort (ie support a less-than concept), and it has several data items such that I need to do lexicographic ordering then I need something like this: struct MyData { string surname; string forename; bool operator<(const MyData& other) const { return surname < other.surname || (surname==other.surname && forename < other.forename); } }; This becomes pretty unmanageable for anything with more than 2 data members. Are there any simpler ways of achieving

Template within template: why “`>>' should be `> >' within a nested template argument list”

喜夏-厌秋 提交于 2019-11-26 22:37:16
I know that when we are using template inside another template, we should write it like this: vector<pair<int,int> > s; and if we write it without the whitespace: vector<pair<int,int>> s; we will get an error: `>>' should be `> >' within a nested template argument list I see this is understandable, but I just can't help but wondering, in which cases will this be really ambiguous? Sometimes you want it to be >> . Consider boost::array<int, 1024>>2> x; In C++03 this successfully parses and creates an array of size 256 . It won't ever be ambiguous. This is proven by the fact that in C++0x you don

What is lexicographical order?

旧巷老猫 提交于 2019-11-26 21:15:39
问题 What is the exact meaning of lexicographical order? How it is different from alphabetical order? 回答1: lexicographical order is alphabetical order. The other type is numerical ordering. Consider the following values, 1, 10, 2 Those values are in lexicographical order. 10 comes after 2 in numerical order, but 10 comes before 2 in "alphabetical" order. 来源: https://stackoverflow.com/questions/45950646/what-is-lexicographical-order

std::next_permutation Implementation Explanation

廉价感情. 提交于 2019-11-26 15:35:27
I was curious how std:next_permutation was implemented so I extracted the the gnu libstdc++ 4.7 version and sanitized the identifiers and formatting to produce the following demo... #include <vector> #include <iostream> #include <algorithm> using namespace std; template<typename It> bool next_permutation(It begin, It end) { if (begin == end) return false; It i = begin; ++i; if (i == end) return false; i = end; --i; while (true) { It j = i; --i; if (*i < *j) { It k = end; while (!(*i < *--k)) /* pass */; iter_swap(i, k); reverse(j, end); return true; } if (i == begin) { reverse(begin, end);

String Comparison in Java

﹥>﹥吖頭↗ 提交于 2019-11-26 12:50:47
What does "compare two strings lexicographically" mean? Leading from answers from @Bozho and @aioobe, lexicographic comparisons are similar to the ordering that one might find in a dictionary. The Java String class provides the .compareTo () method in order to lexicographically compare Strings. It is used like this "apple".compareTo ("banana") . The return of this method is an int which can be interpreted as follows: returns < 0 then the String calling the method is lexicographically first (comes first in a dictionary) returns == 0 then the two strings are lexicographically equivalent returns

Template within template: why “`>>&#39; should be `> >&#39; within a nested template argument list”

a 夏天 提交于 2019-11-26 08:21:32
问题 I know that when we are using template inside another template, we should write it like this: vector<pair<int,int> > s; and if we write it without the whitespace: vector<pair<int,int>> s; we will get an error: `>>\' should be `> >\' within a nested template argument list I see this is understandable, but I just can\'t help but wondering, in which cases will this be really ambiguous? 回答1: Sometimes you want it to be >> . Consider boost::array<int, 1024>>2> x; In C++03 this successfully parses