【问题标题】:String Selection Sort in C++C++中的字符串选择排序
【发布时间】:2014-06-13 12:50:30
【问题描述】:

在我的选择排序中需要一些关于字符串的帮助。这是我目前所拥有的。

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

//Constant globals
const int NUM_NAMES = 20;

//Function protoypes
void selectionSort(string [], int);
void showArray(const string [] , int);

int main()
{
string names[NUM_NAMES] = {"Collins, Bill", "Smith, Bart", "Allet, Jim",
                           "Griffin, Jim", "Stamey, Marty", "Rose, Geri", 
                           "Taylor, Terri", "Johnson, Jill", 
                           "Aliison, Jeff", "Weaver, Jim", "Pore, Bob", 
                           "Rutherford, Greg", "Javens, Renee", 
                           "Harrison, Rose", "Setzer, Cathy", 
                           "Pike, Gordon", "Holland, Beth"};

char again; //Hold y to repeat

do
{
    //Show array
    cout << "The unsorted values are\n";
    showArray(names, NUM_NAMES);

    //Sort array
    selectionSort(names, NUM_NAMES);

    //Display sorted array
    cout << "The sorted values are\n";
    showArray(names, NUM_NAMES);

    //Run program again?
    cout << "Would you like to run the program again? (Y/N): ";
    cin >> again;
}while(again == 'y' || again == 'Y');
return 0;
}

更新了我的代码,它运行良好。将 minValue 从 int 更改为 string。

void selectionSort(string array[], int NUM_NAMES)
{
int startScan, minIndex;
string minValue;

for(startScan = 0; startScan < (NUM_NAMES -1); startScan++)
{
    minIndex = startScan;
    minValue = array[startScan];
    for(int index = startScan +1; index < NUM_NAMES; index++)
    {
        if (array[index] < minValue)
        {
            minValue = array[index];
            minIndex = index;
        }
    }
    array[minIndex] = array[startScan];
    array[startScan] = minValue;
}
}

如果有人可以在这里帮助我,将不胜感激。

【问题讨论】:

  • 您的selectionSort 函数是否为integers 工作?如果它不适用于整数,那么我建议您在转到字符串版本之前修复它
  • string 具有比较方法和重载运算符。一旦它适用于整数,您就可以替换字符串的方法/运算符。并明确将 minValue 声明为字符串类型变量。

标签: c++ string sorting selection-sort


【解决方案1】:
minValue = array[startScan];

您正在将一个字符串分配给一个 int。将minValue 的类型设为string。那么它应该可以工作。

【讨论】:

  • 谢谢!!!!!!,所以我将 int minValue 更改为 string minValue 并且效果很好。
  • 另一个问题,我决定在程序中结合二分查找。由于名称有空格,我认为使用 cin.get() 会帮助我,但它只会弄乱程序。当我删除空格时它工作正常:Collins_Bill。是否有另一种方法可以准确获取我想不到的我输入的内容、空格和所有内容?
  • 你可能想要getline()。但是在你给出你的整个代码和你遇到的问题之前很难准确地说出来。
  • 工作完美!我猜 cin.get() 更适合字符。
【解决方案2】:

c++ 和 stl 使选择排序更加简单和优雅(字符串或整数)

vector<string> v={"chandler","joey","phoebe","ross","rachel","monica"};
for(auto i=v.begin();i!=v.end();i++)
iter_swap(i, min_element(i,v.end()));
for(auto&x : v) cout<<x<<" ";//printing the sorted vector

如果您不使用 c++14,只需将 auto 更改为向量迭代器。

【讨论】:

    【解决方案3】:

    这可能会有所帮助:

    import java.util.Arrays;
    
    public class StringSort {
        public static void main(String[] args){
    
            final int nu_names = 20;
            String names[ ] = new   String[nu_names];
             names = new String[]{"Collins, Bill", "Smith, Bert", "Allen, Jim",
                                     "Griffin, Jim", "Stamey, Marty", "Rose, Geri",
                                     "Taylor, Terri", "Johnson, Jim",
                                      "Allison, Jeff", "Looney, joe", "Wolfe, Bill",
                                      "James, Jean", "Weaver, Jim", "Pore, Bob",
                                       "Rutherford, Greg", " Javens, Renee",
                                      "Herrison, Rose", "Setzer, Cathy",
                                       "Pike, Gordon", "Holland, Beth"};
    
             sort(names);
             System.out.print(Arrays.toString(names));// print names
    
        }
        //sort method
        public static void sort(String[] names){
            String currentName = "";
            int currenIndex = 0;
            for(int i = 0; i < names.length - 1; i++){
                currentName = names[i];
                currenIndex = i;
                for(int j = i + 1; j < names.length; j++){
                    if(currentName.compareTo(names[j]) > 0) {
                        currentName = names[j];
                        currenIndex = j;
                    }
                }
                 swap(names, i,currenIndex );
            }
        }
         //Swap when necessary
        public static void swap(String[] names, int a, int b){
            String temp = names[a];
            names[a] = names[b];
            names[b] = temp;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-03
      • 2015-05-03
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 2011-11-19
      • 2021-03-17
      • 2018-04-23
      相关资源
      最近更新 更多