使用C ++ Ranges TS将文件从文件加载到std :: set

时间:2017-08-07 15:23:02

标签: c++ c++17

我有一个类的集合,学院,从文件加载。它被分类成一组然后输出。我将代码更改为使用C++ Ranges TS中的std::experimental::ranges::copy()作为输出。如何更改代码以使用范围将数据加载到文件中的std::set?我无法找到任何这种用法的例子。

cat >r.cpp <<EOF
#include <sstream>
#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <iterator>
#include <experimental/ranges/algorithm>
#include <experimental/ranges/iterator>

class College {
   std::string id_;
   std::string name_;
public:
   College() : id_("junk"), name_("Junk U.") { }
   College(std::string& id_a, std::string& name_a) : id_(id_a), name_(name_a) {name_.resize(name_.size());}
   friend std::ostream& operator<<(std::ostream& os_a, const College& college_a) {
      return os_a << college_a.id_ << ' ' << college_a.name_;
      }
   friend std::istream& operator>>(std::istream& is_a, College& college_a) {
      char junk;
      return is_a >> college_a.id_ >> std::noskipws >> junk, std::getline(is_a,college_a.name_);// name: 1st space to newline.
      }
   friend bool operator<(const College& lhs_a, const College& rhs_a) {
      return lhs_a.name_ < rhs_a.name_;
      }
   };

namespace ranges = std::experimental::ranges;
const std::string CEEB_WORKING_TXT {"ceeb-working.txt"};

int main(int argc, char **argv) {
   std::ifstream ifs {CEEB_WORKING_TXT, std::ios::in};
   std::istream_iterator<College> ii {ifs}, ii_end {};
   std::set<College> colleges{};
   std::copy(ii, ii_end, std::inserter(colleges, colleges.begin()));
   ranges::copy(colleges, ranges::ostream_iterator<College>(std::cout, "\n"));
   return 1;
   }
EOF
cat >ceeb-working.txt <<EOF
0707 Rowan Technical College
0980 University of Saskatchewan
1058 Belmont University
EOF
g++-7 -or -std=c++1z -fconcepts r.cpp
./r

我正在使用CaseyCarter/cmcstl2实现C ++ Extensions for Ranges下载4Aug&#39; 17和

Linux rome 4.10.0-22-generic #24-Ubuntu SMP Mon May 22 17:43:20 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
gcc version 7.0.1 20170407 (experimental) [trunk revision 246759] (Ubuntu 7-20170407-0ubuntu2)

0 个答案:

没有答案