21

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

我被告知要使用它,但我无法弄清楚我做错了什么。

// std::sort(stringarray.begin(), stringarray.end());

#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;
}
4

9 回答 9

31

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

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

如果您不使用 C++11 并且没有std::begin()and 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;
}
于 2013-08-17T19:43:06.243 回答
14
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;
}

编辑 :

考虑到所有“正确”的命名约定(根据评论):

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;
}

注意: Dietmar Kühl 的答案在所有方面都是最好的,std::begin()&std::end()应该用于std::sort与 C++11 类似的函数,否则可以定义它们。

于 2013-08-17T19:43:02.417 回答
9

使用 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;

}

http://ideone.com/Q9Ew2l

这完全避免了使用普通数组,并允许您使用 std::sort 函数。您可能需要更新编译器以使用= {...}您可以改为使用vector.push_back("name")

于 2013-08-17T19:52:05.893 回答
4

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

相反,如果您可以访问 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;    
}

活生生的例子

于 2013-08-17T19:43:09.023 回答
3

这对我有用:

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

using namespace std;

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

    int sname = sizeof(name)/sizeof(name[0]);

    sort(name, name + sname);

    for(int i = 0; i < sname; ++i)
        cout << name[i] << endl;

    return 0;
}
于 2013-08-17T19:46:06.120 回答
2

正如这里的许多人所说,您可以使用 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 

}

如果要反转排序顺序,只需修改 cmp 函数中的符号即可。

希望这会有所帮助:)

干杯!!!

于 2014-05-20T21:23:05.380 回答
1

多重集容器使用红黑树来保持元素排序。

// using the multiset container to sort a list of strings.
#include <iostream>
#include <set>
#include <string>
#include <vector>


std::vector<std::string> people = {
  "Joe",
  "Adam",
  "Mark",
  "Jesse",
  "Jess",
  "Fred",
  "Susie",
  "Jill",
  "Fred", // two freds.
  "Adam",
  "Jack",
  "Adam", // three adams.
  "Zeke",
  "Phil"};

int main(int argc, char **argv) {
  std::multiset<std::string> g(people.begin(), people.end()); // """sort"""
  std::vector<std::string> all_sorted (g.begin(), g.end());
  for (int i = 0; i < all_sorted.size(); i++) {
    std::cout << all_sorted[i] << std::endl;
  }
}

样本输出:

Adam
Adam
Adam
Fred
Fred
Jack
Jess
Jesse
Jill
Joe
Mark
Phil
Susie
Zeke

请注意,优点是多集在插入和删除后保持排序,非常适合显示活动连接或其他非活动连接。

于 2017-12-27T21:12:35.757 回答
0

我们可以通过 sort() 函数对字符串数组进行排序。

程序 :

  1. 首先确定大小字符串数组。

  2. 使用排序功能。排序(数组名,数组名+大小)

  3. 遍历字符串数组/


代码片段

#include<bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);

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

    int len = sizeof(name)/sizeof(name[0]);

    sort(name, name+len);

    for(string n: name)
    {
         cout<<n<<" ";
    }
    cout<<endl;

    return 0;
}
于 2017-05-27T03:49:39.340 回答
-2

我的解决方案与上述任何解决方案都略有不同,并且在我刚刚运行时就可以正常工作。因此,出于兴趣:

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

using namespace std;

int main() {
  char *name[] = {"john", "bobby", "dear", "test1", "catherine", "nomi", "shinta", "martin", "abe", "may", "zeno", "zack", "angeal", "gabby"};
  vector<string> v(name, name + 14);

  sort(v.begin(),v.end());
  for(vector<string>::const_iterator i = v.begin(); i != v.end(); ++i) cout << *i << ' ';
  return 0;
}
于 2013-08-17T19:53:25.670 回答