0

i am trying to read a file and sorting it alphabetically, i do not know how to tackle this issue.

so i am reading a file that contains , States, counties, and cities. and i need to sort the State alphabetically. can you please guide on how to solve this?

i have tried this:

void sorting(struct state* array, int x){
    int narray = sizeof(array[x].name)/sizeof(array[0].name);
    sort(array[x].name, array[x].name + narray);
    for(int i = 0; i < narray; ++i) {
        cout << array[i].name<< endl;
    }
}

here is my struct:

struct state {
    string name; //name of state
    struct county *c; //name of counties
    int counties; //number of counties in state
    int population; //total population of state
};

and here is how i read the file:

 File >> array[i].name;
 File >> array[i].population;
 File >> array[i].counties;

it gave me an errors, am i on the right track?

the error i got are:

main.cpp:193:45: error: invalid operands to binary expression ('string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') and 'int')
sort(array[x].name, array[x].name + narray);
~~~~~~~~~~~~~ ^ ~~~~~~  

and i got this error 8 times with different numbers:

   /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:3987:1: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. 'int')
operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • 1
    what is the error and on which line? And possible duplicate of this http://stackoverflow.com/questions/1380463/sorting-a-vector-of-custom-objects – Khalil Khalaf Apr 13 '16 at 19:00
  • main.cpp:193:45: error: invalid operands to binary expression ('string' (aka 'basic_string, allocator >') and 'int') sort(array[x].name, array[x].name + narray); ~~~~~~~~~~~~~ ^ ~~~~~~ and i got this error 8 times with differen numbers: – Saad Alarifi Apr 13 '16 at 19:06
  • @SaadAlarifi _`array[x].name + narray`_ What do you actually expect being the result of that statement? It makes completely no sense for me. – πάντα ῥεῖ Apr 13 '16 at 19:21
  • i am sorry if it wasn't clear. so i am reading a file that contains many states and the function sorting is sorting the states alphabetically and printing it out. the code is about 200 lines if would like to see it all. – Saad Alarifi Apr 13 '16 at 19:26
  • What is the format of the data file? – Thomas Matthews Apr 13 '16 at 19:50
  • I highly recommend looking at examples from searching the internet for "StackOverflow c++ read file struct". – Thomas Matthews Apr 13 '16 at 19:51
  • By the way, `sizeof(array[0].name) ` is equivalent to `sizeof(std::string)` which will always be constant since you are asking for the size of the data structure, not the number of characters in the string. I suggest you use `array[0].name.length()` instead. – Thomas Matthews Apr 13 '16 at 19:56
  • To sort a string, use the following: `std::sort(array[x].name.begin(), array[x].name.end());`. *Note: This will change the ordering of the letters in the string*, which may not be what you want. – Thomas Matthews Apr 13 '16 at 19:58

1 Answers1

1

Create comparison operators for struct state using lexicographical_compare

bool operator< (const state& rhs) {
    return lexicographical_compare(name.cbegin(), name.cend(), rhs.name.cbegin(), rhs.name.cend());
}

bool operator== (const state& rhs) {
    return name == rhs.name;
}

You can then use sort directly on array:

sort(begin(array), end(array));

I've trimmed your class a bit, but this is how it would work: http://ideone.com/SwaivI

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • does this mean that the c++ i use doesn't support this? i am really very new when it comes to programming i just got started a few months. i got this error##::::: :::t2.cpp:22:45: error: ‘std::string’ has no member named ‘cbegin’ return lexicographical_compare(name.cbegin(), name.cend(), rhs.name.cbegin(), rhs.name.cend()); – Saad Alarifi Apr 13 '16 at 20:39
  • @SaadAlarifi Sounds like you didn't `#include `. If you look at my live example link, you'll need to also `#include ` and you'll need to either specify the `std` namespace before `string` and `lexographical_compare` or you'll need to do: `using namespace std;`, again look at the example, hopefully you'll find that a helpful guide. – Jonathan Mee Apr 14 '16 at 11:05