C++ 排序字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2803071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ sort array of strings
提问by user69514
I am trying to sort an array of strings, but it's not sorting anything.... what am I doing wrong?
我正在尝试对字符串数组进行排序,但它没有对任何内容进行排序......我做错了什么?
string namesS[MAX_NAMES];
int compare (const void * a, const void * b){
return ( *(char*)a - *(char*)b );
}
void sortNames(){
qsort(namesS, MAX_NAMES, sizeof(string), compare);
}
回答by Puppy
This is C++, not C. Sorting an array of strings is easy.
这是 C++,而不是 C。对字符串数组进行排序很容易。
#include <string>
#include <vector>
#include <algorithm>
std::vector<std::string> stringarray;
std::sort(stringarray.begin(), stringarray.end());
回答by Shiroko
std::qsort
is inherited from the standard C library. It will not work.
std::qsort
继承自标准 C 库。不起作用。
You need to use std::sort
for sorting strings.
您需要std::sort
用于对字符串进行排序。
Specifically, cast std::string
to void*
and then to char*
is undefined and won't work.
具体来说, cast std::string
tovoid*
和 then tochar*
是未定义的,不会工作。
回答by LoudNPossiblyWrong
algorithm sort in CPP has the same complexity as qsort:
CPP 中的算法排序与 qsort 具有相同的复杂度:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
bool compare(string a, string b){
cout << "compare(" << a << "," << b << ")" << endl;
return (a.compare(b) < 0);
}
int main () {
string mystrs[] = {"www","ggg","bbb","ssss","aaa"};
vector<string> myvector (mystrs, mystrs + 5);
vector<string>::iterator it;
sort (myvector.begin(), myvector.end(), compare);
cout << "vector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;
cout << endl;
return 0;
}
回答by Magnetron
You can use boost::sort, like this:
您可以使用 boost::sort,如下所示:
#include <vector>
#include <boost/range/algorithm.hpp>
std::vector<std::string> stringarray;
boost::sort(stringarray);
If you want use find use boost::find, like this:
如果你想使用 find 使用 boost::find,像这样:
std::string findme;
auto offset = boost::find(stringarray, findme) - stringarray.begin()
See 2 useful functions (m_stringarray should be member of ClassA):
查看 2 个有用的函数(m_stringarray 应该是 ClassA 的成员):
const size_t ClassA::GetIdByName(std::string name) const
{
return (boost::find(this->m_stringarray, name) - this->m_stringarray.begin());
}
const std::string ClassA::GetNameById(size_t id) const
{
return this->m_stringarray[id];
}
回答by nelt22
As many here have stated, you could use std::sort to sort, but what is going to happen when you, for instance, want to sort from z-a? This code may be useful
正如这里的许多人所说,您可以使用 std::sort 进行排序,但是例如,当您想要从 za 进行排序时会发生什么?此代码可能有用
bool cmp(string a, string b)
{
if(a.compare(b) > 0)
return true;
else
return false;
}
int main()
{
string words[] = {"this", "a", "test", "is"};
int length = sizeof(words) / sizeof(string);
sort(words, words + length, cmp);
for(int i = 0; i < length; i++)
cout << words[i] << " ";
cout << endl;
// output will be: this test is a
}
If you want to reverse the order of sorting just modify the sign in the cmp function.
如果要颠倒排序顺序,只需修改 cmp 函数中的符号即可。
回答by Sumon Sarker
Here is C++another way to sort array of string without using <vector>.
这是C++另一种不使用<vector>对字符串数组进行排序的方法。
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string WordArray[] = {"AA","DD","CC","BB","ZZ","NN"};
sort(begin(WordArray), end(WordArray)); /*Sort the Array*/
for(auto& Word: WordArray){
cout<<Word<<endl; /*Print Every String Element*/
}
return 0;
}