C 选择排序字符串数组
C++ Selection Sorting String Arrays
我试图了解选择排序如何与字符串一起工作。
这是我到目前为止所拥有的:
#include <iostream>
#include <string>
using namespace std;
// Prototypes
void selectionSort(string arr[], int size);
void showArray(string arr[], int size);
int main() {
const int NUM_NAMES = 20;
string names[NUM_NAMES] = {"Collins, Bill", "Smith, Bart", "Allen, Jim",
"Griffin, Jim", "Stamey, Marty", "Rose, Geri",
"Taylor, Terri", "Johnson, Jill", "Allison, Jeff",
"Looney, Joe", "Wolfe, Bill", "James, Jean",
"Weaver, Jim", "Pore, Bob", "Rutherford, Greg",
"Javens, Renee", "Harrison, Rose", "Setzer, Cathy",
"Pike, Gordon", "Holland, Beth"};
// Insert your code to complete this program
cout << "The names on the list in no particlular order are: ";
showArray(names, NUM_NAMES);
// Calling the sorted array
selectionSort(names, NUM_NAMES);
// Displaying sorted array
cout << "The names in sorted order are: ";
showArray(names, NUM_NAMES);
return 0;
}
// Function to sort the string
void selectionSort(string arr[], int size) {
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++) {
minIndex = startScan;
minValue = arr[startScan];
for (int index = (startScan + 1); index < size; index++) {
if (arr[index] < minValue) {
minValue = arr[index];
minIndex = index;
}
}
arr[minIndex] = arr[startScan];
arr[startScan] = minValue;
}
}
// Function to display the array's conents
void showArray(const int arr[], int size) {
for (int count = 0; count < size; count++) {
cout << arr[count];
cout << "tn";
}
}
如果我尝试构建它会失败,并且会收到一条错误消息。"呼叫'SelectionSort'没有匹配功能。"
但是,如果我尝试使用ints而不是字符串,则可以使其进行编译并正确排序。
#include <iostream>
#include <string>
using namespace std;
// Prototypes
void selectionSort(int [], int);
void showArray(const int [], int);
int main() {
const int NUM_NAMES = 8;
int names[NUM_NAMES] = {1,3,5,7,3,5,7,9};
// Insert your code to complete this program
cout << "The names on the list in no particular order are: ";
showArray(names, NUM_NAMES);
// Calling the sorted array
selectionSort(names, NUM_NAMES);
// Displaying sorted array
cout << "The names in sorted order are: ";
showArray(names, NUM_NAMES);
return 0;
}
// Function to sort the string
void selectionSort(int array[], int size) {
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++) {
minIndex = startScan;
minValue = array[startScan];
for (int index = (startScan + 1); index < size; index++) {
if (array[index] < minValue) {
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
// Function to display the array's contents
void showArray(const int array[], int size) {
for (int count = 0; count < size; count++) {
cout << array[count];
cout << "tn";
}
}
我不确定字符串排序的问题。带有问题的部分C 程序的图像
编译误差是微不足道的。void selectionSort(int [], int);
接受整数数组,它分类为数组。但是您想排序各种字符串。您已声明:
string names[NUM_NAMES];
您必须更改排序功能才能接受字符串数组。修改非常容易,因为std::string
支持=
,<
,>
,==
操作员。
void selectionSort(string arr[], int size)
{
int startScan, minIndex;
string minValue;
for(startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = arr[startScan];
for(int index = (startScan + 1); index < size; index++)
{
if(arr[index] < minValue)
{
minValue = arr[index];
minIndex = index;
}
}
arr[minIndex] = arr[startScan];
arr[startScan] = minValue;
}
}
相关文章:
- 使用 jsoncpp 解析 json 数组字符串
- C++ 对象数组字符串输入在控制台上不起作用
- 插入了 C++ 数组字符串数据,但在显示输出时不显示
- 无法区分 JSON 对象/数组/字符串
- 为什么数组(字符串类型)的大小是 24 字节,带有单个空格元素
- 如何使用数组字符串进行输入验证
- (C++) 打印字符数组字符串的单词时出现问题
- 使用Back_inserter从任何位置的输入字符中使用Back_inserter进行过滤数组字符串
- C 函数反转字符数组字符串
- 如何在 c++ 中创建数组(字符串 [] )的向量
- 如何返回数组字符串的指针
- char数组字符串混淆
- 如何从字符数组字符串中提取空格
- 是否可以将 Bitset<8> 的值复制到数组字符串而不转换它们?
- C++数组字符串函数
- 如何在函数中操作字符数组(字符串)的指针,该指针在C / C ++中作为参数传递
- visual将音频文件存储到数组/字符串流C++中
- c++在char数组(字符串)上将what替换为
- 将数组字符串转换为intger c++(初学者)
- 需要设置数组字符串长度,使其恰好为13