C++ 对字符而不是整个字符串进行排序
C++ Sorting characters rather than entire strings
void selectionSort(string [], int);
void showArray(string [], int);
int main()
{
const int SIZE = 20;
string name[SIZE] =
{"Collins, Bill", "Smith, Bart", "Michalski, Joe", "Griffin, Jim",
"Sanchez, Manny", "Rubin, Sarah", "Taylor, Tyrone", "Johnson, Jill",
"Allison, Jeff", "Moreno, Juan", "Wolfe, Bill", "Whitman, Jean",
"Moretti, Bella", "Wu, Hong", "Patel, Renee", "Harrison, Rose",
"Smith, Cathy", "Conroy, Pat", "Kelly, Sean", "Holland, Beth"};
// Show initial order
cout << "The unsorted values aren";
showArray(name, SIZE);
// Sort the strings
selectionSort(name, SIZE);
// Show ordered order
cout << "The sorted values aren";
showArray(name, SIZE);
return 0;
}
void selectionSort(string name[], int size)
{
char startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = name[startScan].at(0);
for(int index = startScan + 1; index < size; index++)
{
if (name[index].at(0) < minValue)
{
minValue = name[index].at(0);
minIndex = index;
}
}
name[minIndex] = name[startScan];
name[startScan].at(0) = minValue;
}
}
void showArray(string name[], int size)
{
for (int count = 0; count < size; count++)
cout << name[count] << endl;
cout << endl;
}
我希望这个程序显示未排序的字符串数组,对字符串进行排序,显示排序数组。相反,它只是对名称的第一个字母进行排序。这样:
The unsorted values are
Collins, Bill
Smith, Bart
Michalski, Joe
Griffin, Jim
Sanchez, Manny
Rubin, Sarah
Taylor, Tyrone
Johnson, Jill
Allison, Jeff
Moreno, Juan
Wolfe, Bill
Whitman, Jean
Moretti, Bella
Wu, Hong
Patel, Renee
Harrison, Rose
Smith, Cathy
Conroy, Pat
Kelly, Sean
Holland, Beth
The sorted values are
Aollins, Bill
Cmith, Bart
Cichalski, Joe
Griffin, Jim
Hanchez, Manny
Hubin, Sarah
Jaylor, Tyrone
Kaylor, Tyrone
Mmith, Bart
Mmith, Bart
Molfe, Bill
Phitman, Jean
Rmith, Bart
Su, Hong
Shitman, Jean
Su, Hong
Thitman, Jean
Wolfe, Bill
Whitman, Jean
Wu, Hong
我觉得我离我很近...任何帮助,不胜感激。
如果你正在学习算法,编写这样的代码是可以的。
但是请注意,在"实际C++"(即生产代码(中,我们倾向于:
- 使用 STL 容器而不是数组
- 使用 STL 算法而不是原始循环
法典:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
int main(int argc, char** argv)
{
std::vector<std::string> names =
{
"Collins, Bill", "Smith, Bart", "Michalski, Joe", "Griffin, Jim",
"Sanchez, Manny", "Rubin, Sarah", "Taylor, Tyrone", "Johnson, Jill",
"Allison, Jeff", "Moreno, Juan", "Wolfe, Bill", "Whitman, Jean",
"Moretti, Bella", "Wu, Hong", "Patel, Renee", "Harrison, Rose",
"Smith, Cathy", "Conroy, Pat", "Kelly, Sean", "Holland, Beth"
};
std::sort(names.begin(), names.end(), std::less<>());
std::copy(names.begin(), names.end(),
std::ostream_iterator<std::string>(std::cout, "n"));
}
链接:
- 容器库
- 算法库
- 为什么我应该使用容器类而不是简单的数组?
- Scott Meyers:STL 算法与手写循环
使用带有自定义比较功能的 std::sort
-
使用 std::vector 保存 Names 数组
-
定义自定义比较函数
bool myCompare(string i,string j) { if(i.length() >= 1 && j.length() >=1) { return (i[0]<j[0]); } return false; }
-
使用 std::sort
std::sort (vecName.begin(), vecName.end(), myCompare);
在selectionSort
内部,您需要摆脱对.at(0)
的三个调用
当你写name[startScan].at(0)
时,变量name
是一个字符串数组,所以name[startScan]
是一个字符串,所以name[startScan].at(0)
是该字符串的第一个字母。
您需要按字符串进行比较和分配,而不是按字符。 摆脱.at(0)
的将做到这一点,它会给您留下类型错误,需要一些调整,因此char startScan, minIndex, minValue;
应该是
int startScan,minIndex;
string minValue;
- 对字符串进行排序时,在c++中处理sort()
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 按字母顺序对字符串中的字母进行排序,而无需使用内置的 sort()
- 字符串上的合并排序上的 Seg 错误
- 将图形表示为unordered_map<字符串、向量>时拓扑排序错误<string>
- 如何在数组 c++ 中对字符串进行排序
- 提高从排序字符串中获取字母顺序的顺序复杂性
- 尝试使用C++中的模板进行BST排序,但在使用随机字符串进行测试时不起作用
- 如何根据第二列/第三列等对字符串向量进行排序?
- 如何对向量进行排序<浮点数,字符串>而不考虑字符串
- C ++中的字符串排序,也没有在第5个元素正确排序
- C++:使用 LSD 基数排序字符串排序崩溃
- C 样式的字符串排序与排序和 qsort
- 自定义字符串排序
- 编号和字符串排序Al
- 使用STL对子字符串排序
- 如何在c++中按字母顺序对标准输入字符串排序
- c++向量字符串排序不工作
- SQLite 的不区分大小写的 UTF-8 字符串排序规则 (C/C++)