【问题标题】:How to enter string to sort container?如何输入字符串对容器进行排序?
【发布时间】:2016-06-14 06:19:26
【问题描述】:

我有以下字符串: 1465883175.476876 接收 0x03 0x00000000

1465883175.606049 RX 0x00 0x00000000

1465883175.783562 RX 0x02 0x00000000

1465883175.906900 RX 0x03 0x00000000

1465883176.051490 RX 0x00 0x00000000

1465883176.201903 RX 0x03 0x00000000

如何将它们输入到容器中,该容器将根据第一个双类型数据 (c++) 对它们进行排序?

我必须以最有效的方式去做。

我考虑使用 std::set 容器,但在这个容器中,容器中没有两个元素可以具有等效键。在任何情况下,字符串可能包含相同的数字。

【问题讨论】:

  • 正如您提出问题时右侧的弹出框所述:提供详细信息。分享您的研究。
  • 您可以将它们作为字符串放在std::vector 中,并将std::sort 与您自己的比较函数一起使用,该函数解析双精度并比较它们。或者您可以使用带有解析字段的结构并将它们放入向量中,这样您就不必为每次比较解析双精度。
  • @Karsten Koop- 优先队列呢?

标签: c++ sorting c++11 containers


【解决方案1】:
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <queue>

int main() {
  std::vector<std::string> s{
   "1465883175.476876 RX 0x03 0x00000000"
  ,"1465883175.606049 RX 0x00 0x00000000"
  ,"1465883175.783562 RX 0x02 0x00000000"
  ,"1465883175.906900 RX 0x03 0x00000000"
  ,"1465883176.051490 RX 0x00 0x00000000"
  ,"1465883176.201903 RX 0x03 0x00000000"};

  auto cmp = [](const std::string &f, const std::string &s) {
    double n1 = 0.0;
    size_t processed1{0};
    auto sub_str1 = f.substr(0, f.find(" "));
    try {
      n1 = std::stod(sub_str1, &processed1);
      if (processed1 != sub_str1.size()) {
        // TODO
      }
    } catch (std::invalid_argument ia) {
      // TODO
    } catch (std::out_of_range oor) {
      // TODO
    }

    double n2 = 0.0;
    size_t processed2{0};
    auto sub_str2 = s.substr(0, s.find(" "));
    try {
      n2 = std::stod(sub_str2, &processed2);
      if (processed2 != sub_str2.size()) {
        // TODO
      }
    } catch (std::invalid_argument ia) {
      // TODO
    } catch (std::out_of_range oor) {
      // TODO
    }

    return n1 > n2;
  };

  std::priority_queue<std::string, std::vector<std::string>, decltype(cmp)> q(cmp);

  for (const auto &c : s) {
    q.push(c);
  }
  while (!q.empty()) {
    std::cout << q.top() << "\n";
    q.pop();
  }
  return 0;
}

【讨论】:

  • 优先队列不应该有soritinh的键吗?我的意思是我不能给它密钥,它应该通过排序自动嵌入它吗?
  • 优先级队列需要一个比较函数(代码上的cmp),一个priority_queue类似于在一些随机访问容器中管理一个堆,好处是不会意外地使堆失效。 .en.cppreference.com/w/cpp/container/priority_queue
  • 好的,那么有没有容器可以获取一个特定的键并根据这个键(在本例中是数字)进行排序?
  • 另外,它可以升级为对 2 个向量进行排序,而我必须将它们附加到一个向量上?
【解决方案2】:

使用 c++11 编译器支持试试这个代码(你需要改进它):

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

int main() {
  std::vector<std::string> s{
   "1465883175.476876 RX 0x03 0x00000000"
  ,"1465883175.606049 RX 0x00 0x00000000"
  ,"1465883175.783562 RX 0x02 0x00000000"
  ,"1465883175.906900 RX 0x03 0x00000000"
  ,"1465883176.051490 RX 0x00 0x00000000"
  ,"1465883176.201903 RX 0x03 0x00000000"};

  std::sort(s.begin(), s.end(), [](const std::string &f, const std::string &s){
    double n1 = 0.0;
    size_t processed1{0};
    auto sub_str1 = f.substr(0, f.find(" "));
    try {
      n1 = std::stod(sub_str1, &processed1);
      if (processed1 != sub_str1.size()) {
        // TODO
      }
    } catch (std::invalid_argument ia) {
      // TODO
    } catch (std::out_of_range oor) {
      // TODO
    }

    double n2 = 0.0;
    size_t processed2{0};
    auto sub_str2 = s.substr(0, s.find(" "));
    try {
      n2 = std::stod(sub_str2, &processed2);
      if (processed2 != sub_str2.size()) {
        // TODO
      }
    } catch (std::invalid_argument ia) {
      // TODO
    } catch (std::out_of_range oor) {
      // TODO
    }

    return n1 < n2;
  });
  for (const auto &c : s) {
    std::cout << c << "\n";
  }
  return 0;
}

【讨论】:

  • 目的是将向量的元素插入到容器中,容器通过排序插入它们。
  • 相关的是lamda,所以在优先队列中使用它。看一个例子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-17
  • 1970-01-01
  • 2018-10-10
  • 2011-01-23
  • 2020-11-12
  • 2010-09-07
  • 2010-10-21
相关资源
最近更新 更多