简体   繁体   English

我的C ++程序无法正常工作……只是陷入其中,不知道该怎么办

[英]My C++ program is not working …just got stuck in this dont know what to do

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.. im not able to sort the int array accordingly.... but its still not working ...i hope u guys can help 我想从用户那里取值并想对其进行排序...所以我在两个数组中取了一个值,一个是字符串数组,另一个是int数组...如果我对字符串数组进行了排序,它会很好地排序。无法相应地对int数组进行排序....但是它仍然无法正常工作...我希望你们能为您提供帮助

so i searched net and tried using the pair function from STL 所以我搜索了net并尝试使用STL中的pair函数

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*)' [错误]无法将参数'1'的'std :: pair'转换为'const char *'到'int strcmp(const char *,const char *)'

m getting 4 errors of this type 我得到4个此类错误

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: 以下代码使用C ++ 11功能:

#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). 为此,我为一对使用别名,因为稍后在lambda函数排序中将需要它,并且我不希望完整的对<...>文本,它太长了(如您所见,我喜欢简短代码,除了算法库之外,我也不使用合格的名称空间。 Also, I should have used const pair & in the anonymous function, but again, in this situation I give more weight to short code. 另外,我应该在匿名函数中使用const pair & ,但是在这种情况下,我还是更重视短代码。

(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...)) (顺便说一句,C ++ 14甚至允许您使用auto p1, auto p2 。但是我使用了不支持它的Visual Studio 2013(这就是为什么我可以将main()声明为void ++不会让您逃脱)有了这个...))

Then comes the sorting function, with a lambda (or anonymous) function that compares between the strings of each 2 pairs. 然后是排序函数,带有一个lambda(或匿名)函数,用于在每两对字符串之间进行比较。

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++. 我希望这可以更好地理解排序对的方法,并且通常使用C ++。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM