Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8443283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:05:45+00:00 2026-06-10T09:05:45+00:00

Boost documents said that iterator_to gives back a valid iterator but the code below

  • 0

Boost documents said that iterator_to gives back a valid iterator but the code below shows that something other happens.

All indices of Boost.MultiIndex provide a member function called
iterator_to which returns an iterator to a given element of the
container

http://www.boost.org/doc/libs/1_51_0/libs/multi_index/doc/tutorial/indices.html#iterator_to

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/random_access_index.hpp>

using boost::multi_index_container;
using namespace boost::multi_index;

struct Test
{
  int id;
  std::string name;
};

struct idx{};
struct id{};
struct name{};

typedef boost::multi_index_container<
  Test*,
  indexed_by<
    random_access<tag<idx> >,
    ordered_unique<tag<id>, member<Test, int, &Test::id> >,
    ordered_unique<tag<name>, member<Test, std::string, &Test::name> >
  >
> TTestTable;

class TestTable : public TTestTable
{
  public:
    // Fill table with some values
    TestTable()
    {
      Test* test1 = new Test();
      Test* test2 = new Test();
      Test* test3 = new Test();
      Test* test4 = new Test();
      test1->id = 1;
      test2->id = 2;
      test3->id = 3;
      test4->id = 4;
      test1->name = "name1";
      test2->name = "name2";
      test3->name = "name3";
      test4->name = "name4";

      push_back(test1);
      push_back(test2);
      push_back(test3);
      std::cout << at(0)->name << std::endl;
      std::cout << at(1)->name << std::endl;
      std::cout << at(2)->name << std::endl;

      typedef TTestTable::index<idx>::type test_table_by_index;
      test_table_by_index::iterator it = get<0>().iterator_to(test1); // gives back a wrong iterator

      std::cout << get<idx>().iterator_to(test1) - get<idx>().begin() << "\n"; // WRONG
      replace(iterator_to(test1), test4); // CRASH
      replace(it, rule4); // CRASH
    }
};

int
main(int argc, char **argv)
{
  TestTable table;

   return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:05:46+00:00Added an answer on June 10, 2026 at 9:05 am

    From the link you provided:

    // The following, though similar to the previous code,
    // does not work: iterator_to accepts a reference to
    // the element in the container, not a copy.
    int x=c.back();
    c.erase(c.iterator_to(x)); // run-time failure ensues
    

    You’re not passing a reference into iterator_to, you’re passing a copy.

    The following should work:

    Test* const& test1_ref = at(0);
    
    typedef TTestTable::index<idx>::type test_table_by_index;
    test_table_by_index::iterator it = get<0>().iterator_to(test1_ref);
    
    std::cout << get<idx>().iterator_to(test1_ref) - get<idx>().begin() << "\n";
    replace(iterator_to(test1_ref), test4);
    

    As an aside, I don’t think boost::multi_index_containers are designed to be used as base classes since they don’t provide a virtual destructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to boost certain documents. But they dont get boosted. Please tell
When indexing, I boost certain documents, but they do not appear on the top
Boost Network I am trying out this code in a small console app on
If I've given a boost value for documents at index time, can I disable/ignore
I am trying to port a windows project that includes boost 1.37 and wxwidgets
I try to compile the boost filesystem tutorial in Code::Blocks And it comes up
I am using boost 1.45.0 on Ubuntu with Code::Blocks as my IDE, and I
How can I boost the score for documents in which my query matches a
I'm using ElasticSearch to index documents. My mapping is: mongodocid: { boost: 1.0, store:
I have an optional POD struct that will be contained inside a union. boost::optional<>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.