Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Replace 用迭代器替换多索引复合键_Replace_Composite Key_Multi Index - Fatal编程技术网

Replace 用迭代器替换多索引复合键

Replace 用迭代器替换多索引复合键,replace,composite-key,multi-index,Replace,Composite Key,Multi Index,在boost::multi_索引中是否存在循环索引并执行替换 #include <iostream> #include <string> #include <boost/multi_index_container.hpp> #include <boost/multi_index/composite_key.hpp> #include <boost/multi_index/member.hpp> #include <boost/mu

在boost::multi_索引中是否存在循环索引并执行替换

#include <iostream>
#include <string>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>

using namespace boost::multi_index;
using namespace std;

struct name_record
{

  public:

  name_record(string given_name_,string family_name_,string other_name_)
  {
      given_name=given_name_;
      family_name=family_name_;
      other_name=other_name_;
  }

  string given_name;
  string family_name;
  string other_name;

  string get_name() const { return given_name + " " + family_name + " " + other_name; }

  void setnew(string chg)
  {
      given_name = given_name + chg;
      family_name = family_name + chg;
  }

};

struct NameIndex{};

typedef multi_index_container<
  name_record,
  indexed_by<
    ordered_non_unique<
      tag<NameIndex>,
      composite_key
      <
        name_record,
        BOOST_MULTI_INDEX_MEMBER(name_record,string, name_record::given_name),
        BOOST_MULTI_INDEX_MEMBER(name_record,string, name_record::family_name)
      >
    >
  >
> name_record_set;

typedef boost::multi_index::index<name_record_set,NameIndex>::type::iterator IteratorType;
typedef boost::multi_index::index<name_record_set,NameIndex>::type NameIndexType;

void printContainer(name_record_set & ns)
{
  cout << endl << "PrintContainer" << endl << "-------------" << endl;
  IteratorType it1 = ns.begin();
  IteratorType it2 = ns.end  ();

  while (it1 != it2)
  {
       cout<<it1->get_name()<<endl;
       it1++;
  }
  cout << "--------------" << endl << endl;

}


void modifyContainer(name_record_set & ns)
{
  cout << endl << "ModifyContainer" << endl << "-------------" << endl;
  IteratorType it3;
  IteratorType it4;

  NameIndexType & idx1 = ns.get<NameIndex>();
  IteratorType it1 = idx1.begin();
  IteratorType it2 = idx1.end();

  while (it1 != it2)
  {
        cout<<it1->get_name()<<endl;
        name_record nr = *it1;

        nr.setnew("_CHG");
        bool res = idx1.replace(it1,nr);
        cout << "result is: " << res << endl;
        it1++;
  }
  cout << "--------------" << endl << endl;
}

int main()
{
  name_record_set ns;

  ns.insert( name_record("Joe","Smith","ENTRY1")          );
  ns.insert( name_record("Robert","Brown","ENTRY2")       );
  ns.insert( name_record("Robert","Nightingale","ENTRY3") );
  ns.insert( name_record("Marc","Tuxedo","ENTRY4")        );

  printContainer  (ns);
  modifyContainer (ns);
  printContainer  (ns);

  return 0;
}



PrintContainer
-------------
Joe Smith ENTRY1
Marc Tuxedo ENTRY4
Robert Brown ENTRY2
Robert Nightingale ENTRY3
--------------


ModifyContainer
-------------
Joe Smith ENTRY1
result is: 1
Marc Tuxedo ENTRY4
result is: 1
Robert Brown ENTRY2
result is: 1
--------------


PrintContainer
-------------
Joe_CHG Smith_CHG ENTRY1
Marc_CHG Tuxedo_CHG ENTRY4
Robert Nightingale ENTRY3
Robert_CHG Brown_CHG ENTRY2
--------------
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间boost::multi_索引;
使用名称空间std;
结构名称\u记录
{
公众:
名称\记录(给定字符串\名称\字符串系列\名称\字符串其他\名称\字符串)
{
给定名称=给定名称;
family_name=family_name;
其他名称=其他名称;
}
给定名称的字符串;
字符串族名称;
其他名称的字符串;
字符串get_name()常量{返回给定的_name+“”+家族_name+“”+其他_name;}
void setnew(字符串chg)
{
给定名称=给定名称+chg;
family_name=family_name+chg;
}
};
结构名称索引{};
typedef多索引容器<
姓名/记录,
索引<
有序非唯一<
标签,
复合键
<
姓名/记录,
BOOST\u多索引成员(名称记录、字符串、名称记录::给定名称),
BOOST\u多索引成员(名称记录、字符串、名称记录::家族名称)
>
>
>
>名称\记录\集合;
typedef boost::multi_index::index::type::iterator IteratorType;
typedef boost::multi_index::index::type NameIndexType;
无效打印容器(名称\记录\集合(&N)
{
库特
如果您以前没有使用它,
make\u counting\u迭代器
位只是用迭代器填充向量到
ns
的所有元素的一种紧凑方式

while (it1 != it2)
{
      cout<<it1->get_name()<<endl;
      name_record nr = *it1;

      nr.setnew("_CHG");
      bool res = idx1.replace(it1,nr);
      cout << "result is: " << res << endl;
      it1++;
}
#include <vector>
#include <boost/iterator/counting_iterator.hpp>

void modifyContainer(name_record_set & ns)
{
  cout << endl << "ModifyContainer" << endl << "-------------" << endl;

  NameIndexType & idx1 = ns.get<NameIndex>();
  std::vector<IteratorType> v(
    boost::make_counting_iterator(idx1.begin()),
    boost::make_counting_iterator(idx1.end()));
  std::vector<IteratorType>::iterator it1=v.begin(),
                                      it2=v.end();

  while (it1 != it2)
  {
        cout<<(*it1)->get_name()<<endl;
        name_record nr = **it1;

        nr.setnew("_CHG");
        bool res = idx1.replace(*it1,nr);
        cout << "result is: " << res << endl;
        it1++;
  }
  cout << "--------------" << endl << endl;
}