【问题标题】:How can I build a std::vector<std::string> and then sort them?如何构建 std::vector<std::string> 然后对它们进行排序?
【发布时间】:2010-10-15 20:10:07
【问题描述】:

我有一堆字符串需要排序。我认为 std::vector 是最简单的方法。但是,我以前从未使用过向量,因此需要一些帮助。

我只需要按字母数字对它们进行排序,没什么特别的。事实上,string::compare 函数可以工作。

之后,我如何遍历它们以验证它们是否已排序?

这是我目前所拥有的:

std::sort(data.begin(), data.end(), std::string::compare);

for(std::vector<std::string>::iterator i = data.begin(); i != data.end(); ++i)
{
    printf("%s\n", i.c_str);
}

【问题讨论】:

标签: c++ string sorting vector


【解决方案1】:

你可以这样做

std::sort(data.begin(), data.end());

它会对你的字符串进行排序。然后通过它们检查它们是否有序

if(names.empty())
    return true; // empty vector sorted correctly
for(std::vector<std::string>::iterator i=names.begin(), j=i+1; 
        j != names.end(); 
        ++i, ++j)
    if(*i > *j)
        return false;
return true; // sort verified

特别是,std::string::compare 不能用作比较器,因为它不会做sort 想要做的事情:如果第一个参数小于第二个参数,则返回 true,否则返回 false。如果你像上面那样使用sort,它只会使用operator&lt;,它会做到这一点(即std::string使它返回first.compare(second) &lt; 0)。

【讨论】:

  • 只是为了好玩(并且未经测试):检查向量是否已排序可以简化为 std::adjacent_find(names.begin(), names.end(), std::greater()) == names.end()
【解决方案2】:

问题到底是什么?似乎一切都已经存在了。

但是,您可能应该使用std::cout &lt;&lt; *i &lt;&lt; std::endl;

  1. i 是一个迭代器 == 指向容器中数据的指针,所以需要*
  2. c_str()std::string 的函数,而不是变量

您的代码中的问题与您的问题无关?

给你的一些提示:

  • std::vector 也覆盖了 [] 运算符,因此您可以省去迭代器的麻烦并将其用作数组(从 0 迭代到 vector.size())。
  • 您可以改用std::set,它会在插入时自动排序(二叉树),因此您可以节省额外的排序。
  • 使用仿函数让您的输出更加有趣:copy(V.begin(), V.end(), ostream_iterator&lt;std::string&gt;(cout, "\n"));

【讨论】:

    【解决方案3】:

    litb 一如既往地正确。

    我只是想指出一个更普遍的观点——任何可以与都可以用std::sort进行排序。我有时会偷偷将一个 operator

    【讨论】:

      【解决方案4】:

      用于排序:
      std::sortstd::vector&lt; std::string&gt;::sort(..) 方法。
      检查是否已排序:
      使用 std::is_sorted 对检查进行排序 - http://www.sgi.com/tech/stl/is_sorted.html

      std::adjacent_find( v.begin(), v.end(), std::greater&lt; std::string &gt;() ) == v.end()

      对于您的情况,您可以使用默认比较器

      已编辑:
      std::is_sorted 不是标准 stl 函数,它在 sgi stl 实现中定义。
      感谢@Brian Neal 的说明。

      【讨论】:

      • 您不应该依赖该 sgi 站点来获取有关 STL 的信息。它早于标准。 is_sorted 不是标准的。
      • 如果我错了,请纠正我:is_sorted 是在 c++11 中添加的 en.cppreference.com/w/cpp/algorithm/is_sorted
      【解决方案5】:

      您可以使用std::set,它自然是一个排序容器。

      【讨论】:

        【解决方案6】:

        对字符串进行排序:

        using namespace std; // to avoid using std everywhere 
        std::sort(data.begin(), data.end()); // this will sort the strings
        

        检查向量是否排序:

        if(vec.empty())
            return true; // empty vector is sorted correctly
        for(std::vector< std::string>::iterator i=vec.begin(), j=i+1; j != vec.end(); ++i, ++j)
            if(*i > *j)  return false;
        return true; // sort verified
        

        C++11 检查排序向量的方法: std::is_sorted(vec.begin(),vec.end())

        现在打印排序后的向量:

           for(std::vector< std::string>::iterator i = vec.begin(); i != vec.end(); ++i)
        {
            std::cout<< *i <<std::endl;
        }
        

        【讨论】:

        • 我已经检查了代码块和 ideone,这很好用。
        • 从C++11开始,检查向量是否排序很简单std::is_sorted(vec.begin(),vec.end())
        • 您不应该依赖该 sgi 站点来获取有关 STL 的信息。它早于标准。 is_sorted 不是标准的
        • is_sorted 不是标准的
        • AFAIK, is_sorted 是在 C++11 中添加的
        【解决方案7】:

        尝试使用comaprator:

         #include <cmath>
         #include <cstdio>
         #include <vector>
         #include <iostream>
         #include <algorithm>
         using namespace std;
        
        //comparing function only sorts if string size is equal and keeps the larger integgers at last.
        bool myfunction (string i,string j) 
        { 
        int n=i.length();
        int m=j.length();
        if(n==m)
            return (i<j);
        
        return n<m;   
          }
        
        
        int main() {
        int n;
        cin>>n;
        vector <string> arr(n);
        for(int i=0;i<n;i++)
            cin>>arr[i];
        
        
        sort(arr.begin(),arr.end(),myfunction);
        
        for(int i=0;i<n;i++)
            cout<<arr[i]<<endl;
        
        return 0;
         }
        

        【讨论】:

          猜你喜欢
          • 2011-06-04
          • 2020-09-07
          • 2010-09-19
          • 1970-01-01
          • 2011-10-29
          • 1970-01-01
          • 2021-01-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多