C++字符串数组排序

ssj*_*878 16 c++ arrays sorting

我在尝试从C++库中找出排序函数并尝试从az对这个字符串数组进行排序时遇到了很多麻烦,请帮助!

我被告知使用这个,但我无法弄清楚我做错了什么.

// std::sort(stringarray.begin(), stringarray.end());
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
  int z = 0;
  string name[] = {"john", "bobby", "dear", 
                   "test1", "catherine", "nomi", 
                   "shinta", "martin", "abe", 
                   "may", "zeno", "zack", "angeal", "gabby"};

  sort(name[0],name[z]);

  for(int y = 0; y < z; y++)
  {
    cout << name[z] << endl;
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

Die*_*ühl 28

算法使用迭代器到序列的开头和结尾.也就是说,你想要调用std::sort()这样的东西:

std::sort(std::begin(name), std::end(name));
Run Code Online (Sandbox Code Playgroud)

如果您不使用C++ 11并且没有使用std::begin()std::end(),它们很容易定义(显然不在命名空间中std):

template <typename T, std::size_t Size>
T* begin(T (&array)[Size]) {
    return array;
}
template <typename T, std::size_t Size>
T* end(T (&array)[Size]) {
    return array + Size;
}
Run Code Online (Sandbox Code Playgroud)


P0W*_*P0W 11

int z = sizeof(name)/sizeof(name[0]); //Get the array size

sort(name,name+z); //Use the start and end like this

for(int y = 0; y < z; y++){
    cout << name[y] << endl;
}
Run Code Online (Sandbox Code Playgroud)

编辑:

考虑所有"适当的"命名约定(根据评论):

int N = sizeof(name)/sizeof(name[0]); //Get the array size

sort(name,name+N); //Use the start and end like this

for(int i = 0; i < N; i++){
    cout << name[i] << endl;
}
Run Code Online (Sandbox Code Playgroud)

注:迪特马尔·库尔的答案最好是在所有方面,std::begin()std::end()应该用于std::sort像函数与C++ 11,否则他们可以被定义.

  • _Please_不要像这样获得数组大小. (4认同)
  • 对于未来程序员的爱:至少,将`z`重命名为`N`,将`y`重命名为`i`,以便它至少类似于使用索引操作的所有其他代码... (3认同)

ole*_*ard 8

使用std :: vector的示例

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

int main()
{
    /// Initilaize vector using intitializer list ( requires C++11 )
    std::vector<std::string> names = {"john", "bobby", "dear", "test1", "catherine", "nomi", "shinta", "martin", "abe", "may", "zeno", "zack", "angeal", "gabby"};

    // Sort names using std::sort
    std::sort(names.begin(), names.end() );

    // Print using range-based and const auto& for ( both requires C++11 )
    for(const auto& currentName : names)
    {
        std::cout << currentName << std::endl;
    }

    //... or by using your orignal for loop ( vector support [] the same way as plain arrays )
    for(int y = 0; y < names.size(); y++)
    {
       std:: cout << names[y] << std::endl; // you were outputting name[z], but only increasing y, thereby only outputting element z ( 14 )
    }
    return 0;

}
Run Code Online (Sandbox Code Playgroud)

http://ideone.com/Q9Ew2l

这完全避免使用普通数组,并允许您使用std :: sort函数.您可能需要更新编译器以使用= {...}您可以使用而不是添加它们vector.push_back("name")


Tem*_*Rex 6

您的循环不会执行任何操作,因为您的计数器z为 0(并且 0 < 0 的计算结果为false,因此循环永远不会启动)。

相反,如果您可以访问 C++11(并且您确实应该以此为目标!),请尝试使用迭代器,例如通过使用非成员函数std::begin()andstd::end()以及 range-for 循环来显示结果:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() 
{
    int z = 0;
    string name[] = {"john", "bobby", "dear", "test1", "catherine", "nomi", "shinta", "martin", "abe", "may", "zeno", "zack", "angeal", "gabby"};

    sort(begin(name),end(name));

    for(auto n: name){
         cout << n << endl;
    }
    return 0;    
}
Run Code Online (Sandbox Code Playgroud)

活生生的例子