简体   繁体   English

集合上的输出迭代器:分配和递增迭代器

[英]Output iterator on a set: assign and increment iterator

There are many questions about output iterator and sets but none of them address this particular topic. 关于输出迭代器和集合有很多问题,但是没有一个解决这个特定主题的。

I writing a function to deserialize a sequence of objects to a container. 我编写了将对象序列反序列化到容器的函数。 I wish this function be as general as possible, so container can be either a set or a vector. 我希望此功能尽可能通用,因此容器可以是集合或向量。

I'm thinking on receiving an OutputIterator as argument where to put each new object as they are deserialized. 我正在考虑接收OutputIterator作为参数,在每个新对象进行反序列化时将其放置在何处。 I'm not sure what to expect when writing to the iterator if the container is a set. 如果容器是一个集合,我不确定在写迭代器时会发生什么。

To be brief, let say the containers in this example hold int instead of pointers to objects, returned one by one by getIntFromInput() . 简而言之,假设此示例中的容器保存int而不是指向对象的指针,这些对象由getIntFromInput()逐一返回。

void deserializeToContainer(OutputIterator oit){
  while(moreInputAvailable()){
    *oit = getIntFromInput();
    oit++;
  }
}

void deserializeToVector(vector<int> &vectorContainer){
  deserializeToContainer(vectorContainer.back_inserter());
}

void deserializeToSet(set<int> &setContainer){
  deserializeToContainer(setContainer.end());
}

Since set hasn't a back_inserter, and elements aren't stored in the order they are inserted, I wonder what to expect about *oit assignment and oit increment when the container is a set, like in deserializeToSet 由于set没有back_inserter,并且元素没有按照插入的顺序存储,所以我想知道当容器为set时对*oit赋值和oit增量会有什么期望,例如在deserializeToSet

Thank you very much. 非常感谢你。

You can use a std::insert_iterator (probably with std::inserter ) 您可以使用std::insert_iterator (可能与std::inserter std::insert_iterator一起使用)

void deserializeToSet(set<int> &setContainer){
  deserializeToContainer(std::inserter(setContainer, setContainer.end()));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM