Sorting vector of string pointers - c++

I'm sorry if this has already been asked somewhere, but I haven't been able to find an answer to what I'm looking for.
I have a vector of std::string pointers, that I want to sort in alphabetical order, and I haven't been able to figure out how to do this. I am using std::sort.
I wrote up a quick program to test out what I was trying to do (since in the actual implementation, my code is being run in a child process, so it's kind of hard to debug):
#include <string>
#include <algorithm>
#include <vector>
#include <string.h>
bool cmpStrPtrs(std::string *a, std::string *b) {
std::string a1 = *a;
std::string a2 = *b;
if(a1 == a2) return 0;
return a1 > a2 ? 1 : -1;
}
int main(int argc, char *argv[]) {
std::vector<std::string *> vec;
std::string *str1 = new std::string("AAAAA");
std::string *str2 = new std::string("aaaaa");
std::string *str3 = new std::string("xxxxx");
std::string *str4 = new std::string("bfuen");
std::string *str5 = new std::string("xylophone");
vec.push_back(str1);
vec.push_back(str2);
vec.push_back(str3);
vec.push_back(str4);
vec.push_back(str5);
std::sort(vec.begin(), vec.end(), cmpStrPtrs);
for(std::string *str : vec) {
printf("%s\n", str->c_str());
}
return 0;
}
When I run this, I get this output:
$ ./strsort
xylophone
bfuen
xxxxx
aaaaa
AAAAA
This doesn't seem to be in any sort of alphabetical order at all, so I can assume that I'm either using sort() wrong or there's something wrong with my comparator function. I also tried it without a comparator function and I think that's just ordering them based on their memory locations from least to greatest, which doesn't actually change anything. I also tried using
bool cmpStrPtrs(std::string *a, std::string *b) {
return a->compare(*b);
}
but it gave me the same result.
If it's relevant, I'm compiling with g++ using the c++17 standard.

std::string::compare returns an int, not a bool. According to cppreference.com the return value is
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 strong text
The returned value is cast to bool which evaluates to true for all non-zero values. That means that your function returns true for every non-identical pair of strings.
The C++ standard actually defines operator< for strings so you can change your function to
bool cmpStrPtrs(std::string *a, std::string *b) {
return *a < *b;
}
But that still leaves a big issue in your code. You absolutely do not need pointers for this. In fact, you are leaking memory right now because you neglect to delete them. The proper tool for this job is std::vector<std::string>. This has the added benefit that without the extra level of indirection, std::sort can implicitly call operator< without a helper function, leading to the following solution.
std::vector<std::string> vec;
vec.emplace_back("AAAAA");
vec.emplace_back("aaaaa");
vec.emplace_back("xxxxx");
vec.emplace_back("bfuen");
vec.emplace_back("xylophone");
std::sort(vec.begin(), vec.end());

You can do it with a lambda:
std::sort(vec.begin(), vec.end(), [](std::string * a, std::string * b) {
return *a < *b;
});

Your comparison function is meant to simulate the less-than operator - that means it should return true if a comes before b. Your current implementation returns true if a doesn't equal b.
You have:
if(a1 == a2) return 0;
return a1 > a2 ? 1 : -1;
which should be:
if(a1 == a2) return false;
return a1 > a2 ? false : true;
or just:
return a1 < a2;

std::sort expects Strict Weak Ordering. It doesn't give a crap about equals; it only cares about before and after.
The comparison function should return true if the right hand side goes before left hand side. Unfortunately in
bool cmpStrPtrs(std::string *a, std::string *b) {
std::string a1 = *a;
std::string a2 = *b;
if(a1 == a2) return 0;
return a1 > a2 ? 1 : -1;
}
bool is true for any value that is not 0. This means that both greater than and less than are true. This makes logical ordering pretty much impossible because greater than and less than go before.
Improvement cut 1: return bool based on lexicographic (alphabetical) ordering. String already implements a less than operator that does exactly what you want. Let's use it.
bool cmpStrPtrs(std::string *a, std::string *b) {
std::string a1 = *a;
std::string a2 = *b;
return a1 < a2;
}
Improvement cut 2: std::string a1 = *a; creates a brand new string that is a copy of the original. Since you have a pointer to the original you can dereference the pointer and use the original. No need for the copy.
bool cmpStrPtrs(std::string *a, std::string *b) {
return *a < *b;
}

Related

How to compare a vector of const char[]

I have a vector that is composed of const char[] and I want to be able to check if each character array is equal to the rest in the vector. So if this is how I initialize, and then I assign a random const char[] to 5 parts of the vector array, how can I compare them without using ==?
const char sled[]="sled";
const char car[]="car";
const char house[]="house";
const char dog[] ="dog";
vector<const char[]> vect;
if (vect[0]== vect[1]== vect[2]. == vect[3] == vect[4])
{
cout << "They are all equal!"
return;
}
const char * or C-strings must be compared with strcmp(). You can write your own for loop that compares the chars one by one as well.
Your attempt to use C-style strings with std::vector won't even compile, but assuming you find a way to make it work, here's how I would compare two strings, making sure that no operator == is ever used (considering that strcmp might make use of that operator in its implementation):
bool eq(char a, char b) {
return !(static_cast<int>(a) - static_cast<int>(b));
}
bool are_equal(const char* a, const char* b) {
int i = 0;
for (; !eq(a[i], '\0'); ++i)
if (!eq(a[i], b[i]))
return false;
return (eq(b[i], '\0'));
}
And here's a live example of it's execution. Now, if you wish to actually make things the right way, here's how you would do it:
std::vector<std::string> vect;
vect.emplace_back("sled");
vect.emplace_back("car");
vect.emplace_back("house");
vect.emplace_back("dog");
if (std::all_of(
vect.begin() + 1,
vect.end(),
[&](const std::string& s) {
return s == vect.front();
}
)) {
std::cout << "They are all equal!";
}
And here's a live example of that too.
Have a nice coding day.

Using char* as a key in std::map, how does it work

This question relates directly to using char as a key in stdmap.
I understand what the compare function passed in does and why its required for char * types as a key. However, I'm uncertain as how the updating actually works.
I'm curious as to the case where you are updating a key. How does std::map know how to compare equality between the const char *, cmp_str only tells map the order in which to inserted keys into the tree.
I've done a little digging into the stl_tree.h code (pulled from here) but wasn't able to find much. My only guess is that its doing a straight memory comparison.
I'm interested in how the underling stl_tree class handles this situation, or if it doesn't handle it correctly all the time, what edge case breaks?
Code
#include <map>
#include <iostream>
#include <cstring>
struct cmp_str
{
bool operator()(char const *a, char const *b)
{
return std::strcmp(a, b) < 0;
}
};
int main ( int argc, char ** argv )
{
std::map<const char*, int, cmp_str> map;
map["aa"] = 1;
map["ca"] = 2;
map["ea"] = 3;
map["ba"] = 4;
map["ba"] = 5;
map["bb"] = 6;
map["ba"] = 7;
std::map<const char*, int, cmp_str>::iterator it = map.begin();
for (; it != map.end(); it++ )
{
std::cout << (*it).first << ": " << (*it).second << std::endl;
}
return 0;
}
Output
aa: 1
ba: 7
bb: 6
ca: 2
ea: 3
The ordered containers all use equivalence classes: Two values a and b are considered equivalent if neither one is smaller than the other: !(a < b) && !(b < a) or, if you insist on the notation using a binary predicate !pred(a, b) && !pred(b, a).
Note, that you need to keep the pointers live in your map: if the pointers go out of scope you will get strange results. Of course, string literals stay valid throughout the life-time of the program.
Well, cmp_str can be used to find identical keys. If both cmp_str::operator(x,y) and cmp_str::operator(y,x) return false, you've found a duplicate key. There's really not much more to it.

C++ Sorting Class Array

C++ Sorting Array Class
I have an array object that record the following..
This is at classone.h
ClassOne
{
string name;
int data;
float valueData;
}
and the constructor are created at classone.cpp
At main.cpp I created ClassOne Array of Size 10
#include "classone.h"
ClassOne cone[10];
Next is i recorded several value to the object
and now ClassOne got 3 objects
cone[0]
name = "hello"
data = 1
valueData = 20
cone[1]
name = "panda"
data = 2
valueData = 15
cone[2]
name = "joe"
data = 3
valueData = 25
What i want to achieve is do a sort that can rearrange this array by valueData highest ascending form so.. it will be
cone[2] then cone[0] then cone[1] ..
but the issue if i use bubble sort , i tried google and find some, they are sorting by e.g int a[]={9,6,5,23,2,6,2,7,1,8};
but i wanna sort by class array object. and re-arrange the value together , how do i achieve this.
So when i cout it will be
-- Highest to lowest --
1) Name: Joe , Data = 3, Value =25
2) Name: Hello , Data =1 , Value = 20
3) Name: Panda, Data = 2, Value = 15
Thanks for all help and guide!!
The easiest way is to use the standard library:
#include <algorithm>
std::sort(cone, cone + 10,
[](ClassOne const & a, ClassOne const & b) -> bool
{ return a.value < b.value; } );
If you're willing to define a comparison operator globally, you don't even need the lambda:
bool operator<(ClassOne const & a, ClassOne const & b)
{
return a.value < b.value;
}
std::sort(cone, cone + 10);
Or you could make the comparator a member function. Or you could give the comparator function a custom name and pass that as the third argument of sort. This might be a good idea in the case where the comparison is specific to your situation and not "natural":
bool ValueCmp(ClassOne const & a, ClassOne const & b)
{
return a.value < b.value;
}
std::sort(cone, cone + 10, ValueCmp);
The last version is useful if you don't have C++11 support (for lambdas, as in the first case), or if you want to reuse the comparator in multiple different situations.
Use std::sort and a suitable sort function/functor:
bool comp(const ClassOne& lhs, const ClassOne& rhs)
{
return lhs.valueData < rhs.valueData;
}
std::sort(cone, cone+10, comp);
or, in C++11,
std::sort(std::begin(cone), std::end(cone), comp);
You can make a struct that implements the operator < method that std::sort in the <algorithm> header uses to sort iterated items.
struct One {
string name;
int data;
float valueData;
bool operator < (const one &a) const{
return valueData <a.valueData;
}
};
then all you have to do is to make an array of this struct and sort it using the sort function
Look at your Bubble sort source. At some point, it will be comparing one int to another, probably with either the less than operator (<) or the greater than operator (>). That's where the sort function determines the relative order of those two items. By repeating that comparison many times, the sort function is able to determine the total order of the collection.
You need to replace that operation with your own comparison function. A function that takes two objects of your class, and returns true if the first should be considered less than the second, false if the second should be considered less than the first, and false if they should be considered equivalent.
You must define a comparison operator for your class. How you determine whether one object is less than another isn't clear from your question.
Try this
...
....
void ClassOne::sort(ClassOne *obj,int n)
{
ClassOne temp;
int i, j;
for (i = 0; i < n; i++)
for (j = n - 1; j > i; j--)
if (obj[j].valueData <obj[j - 1].valueData )
{
temp = obj[j];
obj[j] = obj[j - 1];
obj[j - 1] = temp;
}
}
...
int main()
{
ClassOne obj[3],a;
for(int i=0;i<3;i++)
obj[i].readdata();
a.sort(obj,3);
...
}

How to compare C++ string using qsort in c?

I tried to learn the qsort function of the c-library stdlib. This is provided even in c++. But i dont understand how to use them for sorting c++ strings. I am not sure of what the parameters should be for the sizeof() operator and whether my compare_str code is right. I tried this code:
#include<iostream>
#include<cstdlib>
using namespace std;
#include<string>
int compare_str( const void *a, const void *b){
string obj = (const char*)a;
string obj1 = (const char*)b;
return obj.compare(obj1);
}
int main(){
string obj[4] = {"fine", "ppoq", "tri", "get"};
qsort(obj, 4, sizeof(obj[0].length()), compare_str);
for( int i=0; i<4; i++)
cout<<obj[i]<<endl;
return 0;
}
My output was:
ppoq
tri
get
fine
I am not able to make out the error. Please help.
You cannot and must not use qsort on an array of std::strings. The elements must be of trivial type, which strings are not, and thus the behaviour is undefined. From 25.5/4 ("qsort"):
The behavior is undefined unless the objects in the array pointed to by base are of trivial type.
The reason is that qsort will memcpy the array elements around, which is not possible for C++ objects in general (unless they're sufficiently trivial).
If you do have a trivial type, you can use this generic qsorter-comparator (but of course this is a terrible idea, and the inlined std::sort is always preferable):
template <typename T>
int qsort_comp(void const * pa, void const * pb)
{
static_assert<std::is_trivial<T>::value, "Can only use qsort with trivial type!");
T const & a = *static_cast<T const *>(pa);
T const & b = *static_cast<T const *>(pb);
if (a < b) { return -1; }
if (b < a) { return +1; }
return 0;
}
Use: T arr[N]; qsort(arr, N, sizeof *arr, qsort_comp<T>);
Don't use this. Use std::sort instead.
Better be C++ oriented and use std::sort for your array:
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
int main() {
std::string obj[4] = {"fine", "ppoq", "tri", "get"};
std::sort(obj, obj + 4);
std::copy(obj, obj + 4, std::ostream_iterator<std::string>(std::cout, "\n"));
}
AFAIK - std::sort uses quick sort.
[UPDATE] See comments, std::sort is not always pure quick sort.
[UPDATE2]
If you want to learn qsort - change std::string to const char* and define function based on strcmp. Remember that qsort passes pointers to elements in an array - so dereference const void* to get const char*. See:
#include <stdlib.h>
#include <string.h>
int compare_cstr(const void* c1, const void* c2)
{
return strcmp(*(const char**)(c1), *(const char**)(c2));
}
int main() {
const char* obj[4] = {"fine", "ppoq", "tri", "get"};
qsort(obj, 4, sizeof(obj[0]), compare_cstr);
std::copy(obj, obj + 4, std::ostream_iterator<const char*>(std::cout, "\n"));
}
The problem is that you give qsort an array of C++ strings. In your comparison function, you seem to except C strings, since you cast them to (const char*).
Also, the third parameter of qsort, the size of data, you actually give wrong value. sizeof(obj[0].length()) will result in sizeof(size_t), which is obviously wrong. sizeof(obj[0]) would be more correct, but remember that qsort won't call copy constructor of string, which might lead to problems.
I would suggest not to use qsort with C++ strings.
See answer provided by PiotrNycz for correct solution.
You should use the std::sort template function provided by the C++ standard library (in the <algorithm> header file). By default, std::sort uses the less than comparison operator to order elements (std::string already implements operator<). If you need to specify an ordering condition (for example, case insensitive string compare), std::sort allows you to specify an ordering function object.
Example:
#include <string>
#include <algorithm>
bool caseInsensitiveOrdering(const std::string& lhs, const std::string& rhs)
{
// return true if lowercase lhs is less than lowercase rhs
}
int main()
{
std::string names[] = {"chuck", "amy", "bob", "donna"};
size_t nameCount = sizeof(names) / sizeof(names[0]);
// Sort using built-in operator<
std::sort(names, names + nameCount);
// Sort using comparison function
std::sort(names, names + nameCount, &caseInsensitiveOrdering);
}
Your error is in the declaration of the size in qsort. What is expected is the size of a member, which is, in your case, a string. So you want to use:
qsort(obj, 4, sizeof(string), compare_str);
However, you need to work with pointer to string, rather than strings themselves. Then, the code should look like:
int compare_str( const void *a, const void *b){
const string* obj = (const string*)a;
const string* obj1 = (const string*)b;
return obj->compare(*obj1);
}
// ...
string* obj[4] = { new string("fine"), new string("ppoq"),
new string("tri"), new string("get") };
qsort(obj, 4, sizeof(string*), compare_str);
// And delete the objects
for(int i = 0 ; i < 4 ; ++i) delete obj[i];
Works for me:
#include<iostream>
#include<cstdlib>
using namespace std;
#include<string>
int compare_str( const void *a, const void *b){
string* obj = (string*)a;
string* obj1 = (string*)b;
return obj->compare(*obj1);
}
int main(){
string obj[4] = {"fine", "ppoq", "tri", "get"};
qsort(obj, 4, sizeof(string), compare_str);
for( int i=0; i<4; i++)
cout<<obj[i]<<endl;
return 0;
}

How to use a hash_map with case insensitive unicode string for key?

I'm very new to STL, and pretty new to C++ in general. I'm trying to get the equivalent of a .NET Dictionary<string, value>(StringComparer.OrdinalIgnoreCase) but in C++. This is roughly what I'm trying:
stdext::hash_map<LPCWSTR, SomeStruct> someMap;
someMap.insert(stdext::pair<LPCWSTR, SomeStruct>(L"a string", struct));
someMap.find(L"a string")
someMap.find(L"A STRING")
The trouble is, neither find operation usually works (it returns someMap.end()). It seems to sometimes work, but most of the time it doesn't. I'm guessing that the hash function the hash_map is using is hashing the memory address of the string instead of the content of the string itself, and it's almost certainly not case insensitive.
How can I get a dictionary-like structure that uses case-insensitive keys and can store my custom struct?
The hash_map documentation you link to indicates that you can supply your own traits class as a third template parameter. This must satisfy the same interface as hash_compare.
Scanning the docs, I think that what you have to do is this, which basically replaces the use of StringComparer.OrdinalIgnoreCase you had in your Dictionary:
struct my_hash_compare {
const size_t bucket_size = 4;
const size_t min_buckets = 8;
size_t operator()(const LPCWSTR &Key) const {
// implement a case-insensitive hash function here,
// or find something in the Windows libraries.
}
bool operator()(const LPCWSTR &Key1, const LPCWSTR &Key2) const {
// implement a case-insensitive comparison function here
return _wcsicmp(Key1, Key2) < 0;
// or something like that. There's warnings about
// locale plastered all over this function's docs.
}
};
I'm worried though that the docs say that the comparison function has to be a total order, not a strict weak order as is usual for sorted containers in the C++ standard libraries. If MS really means a total order, then the hash_map might rely on it being consistent with operator==. That is, they might require that if my_hash_compare()(a,b) is false, and my_hash_compare()(b,a) is false, then a == b. Obviously that's not true for what I've written, in which case you're out of luck.
As an alternative, which in any case is probably more efficient, you could push all the keys to a common case before using them in the map. A case-insensitive comparison is more costly than a regular string comparison. There's some Unicode gotcha to do with that which I can never quite remember, though. Maybe you have to convert -> lowercase -> uppercase, instead of just -> uppercase, or something like that, in order to avoid some nasty cases in certain languages or with titlecase characters. Anyone?
Also as other people said, you might not really want LPCWSTR as your key. This will store pointers in the map, which means that anyone who inserts a string has to ensure that the data it points to remains valid as long as it's in the hash_map. It's often better in the long run for hash_map to keep a copy of the key string passed to insert, in which case you should use wstring as the key.
There was some great information given here. I gathered bits and pieces from the answers and put this one together:
#include "stdafx.h"
#include "atlbase.h"
#include <map>
#include <wchar.h>
typedef std::pair<std::wstring, int> MyPair;
struct key_comparer
{
bool operator()(std::wstring a, std::wstring b) const
{
return _wcsicmp(a.c_str(), b.c_str()) < 0;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
std::map<std::wstring, int, key_comparer> mymap;
mymap.insert(MyPair(L"GHI",3));
mymap.insert(MyPair(L"DEF",2));
mymap.insert(MyPair(L"ABC",1));
std::map<std::wstring, int, key_comparer>::iterator iter;
iter = mymap.find(L"def");
if (iter == mymap.end()) {
printf("No match.\n");
} else {
printf("match: %i\n", iter->second);
}
return 0;
}
If you use an std::map instead of the non-standard hash_map, you can set the comparison function to be used when doing the binary search:
// Function object for case insensitive comparison
struct case_insensitive_compare
{
case_insensitive_compare() {}
// Function objects overloader operator()
// When used as a comparer, it should function as operator<(a,b)
bool operator()(const std::string& a, const std::string& b) const
{
return to_lower(a) < to_lower(b);
}
std::string to_lower(const std::string& a) const
{
std::string s(a);
std::for_each(s.begin(), s.end(), char_to_lower);
return s;
}
void char_to_lower(char& c) const
{
if (c >= 'A' && c <= 'Z')
c += ('a' - 'A');
}
};
// ...
std::map<std::string, std::string, case_insensitive_compare> someMap;
someMap["foo"] = "Hello, world!";
std::cout << someMap["FOO"] << endl; // Hello, world!
LPCWSTR is a pointer to a null-terminated array of unicode characters and probably not what you want in this case. Use the wstring specialization of basic_string instead.
For case-insensitivity, you would need to convert the keys to all upper case or all lower case before you insert and search. At least I don't think you can do it any other way.