-1

I wanted to take the values from user and wanted to sort it... so i took values in two arrays one in string array and the other in int array... if i sort the string array it get sorted very well.. i m not able to sort the int array accordingly.... but its still not working ...i hope u guys can help

so i searched net and tried using the pair function from STL

void sortItems(char array[][10],int y[],int n){
    pair<char,int>pairt[n];
    for (int i = 0; i < n; i++)  
    { 
        pairt[i].first = array[i][10]; 
        pairt[i].second = y[i]; 
    } 
    char t[20];
    int i,j,k,p=1;

    for(i=1; i<=n; i++)
    {
        for(j=1; j<11; j++)
        {
            if(strcmp(pairt[j-1], pairt[j])>0)
            {
                strcpy(t, pairt[j-1]);                          
                strcpy(pairt[j-1], pairt[j]);               
                strcpy(pairt[j], t);
                p=i;
            }
        }
    }
    std::cout<<"Dokemon in alphabetical order : \n";
    for(i=1; i<=n; i++)
    {
        std::cout<<array[i]<<endl;
    }
}

[Error] cannot convert 'std::pair' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'

m getting 4 errors of this type

Fureeish
  • 12,533
  • 4
  • 32
  • 62
  • 3
    `strcpy` is for copying strings. You have `std::pair`. What was your intention? Also, why not use `std::vector` and `std::sort` from the beginning? – Fureeish Jul 28 '19 at 10:53
  • m doing selection sort in this.. thats is why m using strcpy – Pankaj Mohan Jul 28 '19 at 10:57
  • 1
    `strcpy` has nothing to do with which comparison-based sorting algorithm you have chosen. – Fureeish Jul 28 '19 at 10:58
  • @samini will it make any difference? – Pankaj Mohan Jul 28 '19 at 10:59
  • In the first for loop when you initialize the pairs, do you want to copy character strings from array[] or just a single character? – Pzc Jul 28 '19 at 10:59
  • 2
    This is not valid C++ Code. Your are using a VLA (variable length array). That is not allowed. In general. Do NOT, never and nowhere use plain C-Style arrays. Use STL containers instead. Refactor your code. – A M Jul 28 '19 at 10:59
  • The caller of this is as important as the broken code. The *intent* of this is even more important (and don't say "I want to sort the strings"; there are entirely too many arguments to your function for that task. Finally, there is enough evidence in this code alone to strongly suggest you review whatever text/tutorial you're learning from, specifically the sections on strings, arrays, and zero-based indexing of the latter). This *seems* to suggest you want to sort `y[]` based on the strings in `array[]`, but that's sheer speculation and guesswork on my part. – WhozCraig Jul 28 '19 at 10:59
  • @Fureeish i m actually changing the position of the strings in the array – Pankaj Mohan Jul 28 '19 at 11:01
  • 1
    Please use `std::string` and `std::vector`, it will save you A LOT of problems! After you mastered those, you might not even have the need to learn about C-arrays for a long time. – JVApen Jul 28 '19 at 11:01
  • 1
    FYI: You also have several compiler warnings that indicate bugs, see https://godbolt.org/z/oxyyWb – JVApen Jul 28 '19 at 11:04
  • Seems like an excellent opportunity to learn how to use your debugger. See for example [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl Jul 28 '19 at 11:08
  • @JVApen this code was actually made to just sort the strings... but then i realised i have to sort the other array too.. wrt the values of this array that is why i used pair in this – Pankaj Mohan Jul 28 '19 at 11:12
  • So you had a sorting algorithm that sored c-strings and you changed the string to pair? Yeah, that's not going to work, unless you used `std::string` and `operator <` in the first place. – Fureeish Jul 28 '19 at 11:17

1 Answers1

2

Here is a full solution. I used ad-hoc vectors of strings and numbers, but you can pass them as arguments. The following code uses C++11 features:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

#include <algorithm>

void main()
{
    auto nums = vector < int > {1, 2, 3, 4, 5};
    auto words = vector < string > {"hello", "world", "apples", "oranges", "bananas"}; 

    // collect into pairs
    using MyPair = pair<string, int>;
    vector<MyPair> pairs;
    for (size_t i = 0; i < nums.size(); i++) pairs.push_back(make_pair(words[i], nums[i]));

    // sort in place the pairs according to the words 
    std::sort(pairs.begin(), pairs.end(), [](MyPair p1, MyPair p2) {return p1.first < p2.first; });

    // re-write original vectors
    for (size_t i = 0; i < nums.size(); i++) {
        nums[i] = pairs[i].second;
        words[i] = pairs[i].first;
    }

    // now print and check
    for (size_t i = 0; i < nums.size(); i++) {
        cout << words[i] << ", " << nums[i] << endl;
    }
}

First I create the two vectors. Next I collect them into pairs. For this I use an alias for a pair, since I will need it later in a lambda-function sorting, and I don't want the full pair<...> text, it's too long (as you can see I like short code and I also don't use qualified namespace, except for the algorithm library). Also, I should have used const pair & in the anonymous function, but again, in this situation I give more weight to short code.

(BTW, C++14 would even let you use auto p1, auto p2. But I used Visual Studio 2013 which didn't support it (that's why I could declare main() as void. g++ wouldn't let you get away with this...))

Then comes the sorting function, with a lambda (or anonymous) function that compares between the strings of each 2 pairs.

Eventually I split the pairs back to two vectors. I hope this gives a better understanding of the method of sorting pairs, and in general, using C++.

Israel Unterman
  • 13,158
  • 4
  • 28
  • 35