c++ 如何将姓氏与名字相对应?

neekobn8  于 2022-12-15  发布在  其他
关注(0)|答案(2)|浏览(244)

Output
姓氏是按字母顺序排序的,但它们并不对应于他们的名字。我使用了两个数组来保存我从一个文件中提取的名字/姓氏。
我使用以下方法对姓氏进行排序:'

for(int i= 0; i < LISTSIZE; i++)
{       
minidx = i;  //declared as int before
strcpy(headers[0],lastname[i]);
  
  for(int j = i; j < LISTSIZE; j++)
  {
  int match = strcmp(lastname[j],headers[0]);
  if (match < 0)
  {
  minidx = j;                   
  strcpy(headers[0], lastname[j]);      
  }             
  }
strcpy(headers[0], lastname[minidx]);
strcpy(lastname[minidx], lastname[i]);
strcpy(lastname[i], headers[0]);
  }

'
我试着把姓和名组合成一个数组,但结果什么也没有。我看了多个论坛,没有发现什么值得注意的。我对编程相对较新,所以请原谅我知识不足。

twh00eeo

twh00eeo1#

当从文件中提取时,您可能希望将名字和姓氏放在一个类似结构的东西中,然后按姓氏字段对这些结构进行排序。这会从一开始就将关联的数据放在一起,因此以后无需尝试重新组合。

#include <string>

#define LISTSIZE 3

// Structure that stores a firstname and lastname for a person.
struct person {
    std::string firstname;
    std::string lastname;
};

bool lexical_less_than(std::string wordone, std::string wordtwo) {

    //code to compare two strings and determine which one is 'before'
    //the other in alphabetical order goes here

    return true;
}

int main() {

    //structure initialiser - firstname = bob, lastname = martin
    struct person p1 = {"bob", "martin"};
    struct person p2 = {"jim", "davey"};
    struct person p3 = {"james", "kirk"};
    
    //array to alphabetise
    struct person people[LISTSIZE] = {p1, p2, p3};

    
    //sketch of sorting code - this doesn't work!
    //this is just to demo moving structures around and 
    //accessing fields.
    for (int i = 0; i < LISTSIZE; i++) {
        if (i != LISTSIZE - 1) {
            //pass the lastnames of our people to the compare function
            if (lexical_less_than(people[i].lastname, people[i+1].lastname)) {
                //swap the structures over. Note that you can manipulate 
                //structures in the same way as any other type
                struct person temp = people[i];
                people[i] = people[i+1];
                people[i+1] = temp;
            }
        }
        
    }


}

Structures - MSDNStructures - CPlusPlus上的一些阅读。
也可以考虑使用标准的库算法/容器,比如std::stringstd::vector,而不是char数组和strcpy,这样会更安全,更符合习惯用法。

s5a0g9ez

s5a0g9ez2#

根据这个答案,如果必须使用两个单独的数组,请使用索引数组对这两个数组进行“排序”。
下面是如何使用索引数组的示例:

#include <iostream>
#include <string>
#include <algorithm>

#define LISTSIZE 4

int main()
{
    // Sample first and last name arrays
    std::string firstName[LISTSIZE] = {"Bob", "Alan", "Jack", "Jill"};
    std::string lastName[LISTSIZE] = {"Smith", "Jones", "Brown", "Johnson"};

    // This is the index array
    int index[LISTSIZE] = {0,1,2,3};

    // sort on last name
    std::sort(index, index + LISTSIZE, [&](int n1, int n2)
              { return lastName[n1] < lastName[n2]; });

    // Now print out the names, sorted on last name
    for (int i = 0; i < LISTSIZE; ++i)
       std::cout << firstName[index[i]] << " " << lastName[index[i]] << "\n";
}

输出:

Jack Brown
Jill Johnson
Alan Jones
Bob Smith

注意,我们没有用名字本身对数组进行排序--它们没有被修改。所做的唯一事情是创建索引数组,并根据姓氏的顺序对索引数组进行排序。最后,我们使用位置i处的索引值来关联名字和姓氏。
我知道您使用了C样式的字符串,但原理是相同的。

相关问题