fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <boost/multi_index_container.hpp>
  4. #include <boost/multi_index/ordered_index.hpp>
  5. #include <boost/multi_index/member.hpp>
  6.  
  7. using namespace boost::multi_index;
  8.  
  9. // Define a structure for the data
  10. struct Book {
  11. int rank;
  12. std::string value;
  13.  
  14. Book(int rank, const std::string& value) : rank(rank), value(value) {}
  15. };
  16.  
  17. // Define tags to reference the indices
  18. struct rank_tag {};
  19.  
  20. // Define a multi-index container for Book struct
  21. typedef multi_index_container<
  22. Book,
  23. indexed_by<
  24. ordered_unique<
  25. tag<rank_tag>, // Index by book rank
  26. member<Book, int, &Book::rank>
  27. >
  28. >
  29. > BookMultiIndex;
  30.  
  31. // Function to insert a book and adjust ranks
  32. void insert_book(BookMultiIndex& books, int rank, const std::string& value) {
  33. // Access by rank index
  34. auto& rank_index = books.get<rank_tag>();
  35.  
  36. // Adjust ranks for books with rank >= the new rank
  37. for (auto it = rank_index.lower_bound(rank); it != rank_index.end(); ++it) {
  38. rank_index.modify(it, [](Book& b) {
  39. ++b.rank;
  40. });
  41. }
  42.  
  43. // Insert the new book
  44. books.insert(Book(rank, value));
  45. }
  46.  
  47. // Function to delete a book and adjust ranks
  48. void delete_book(BookMultiIndex& books, int rank) {
  49. // Access by rank index
  50. auto& rank_index = books.get<rank_tag>();
  51.  
  52. // Find the book to delete
  53. auto it = rank_index.find(rank);
  54. if (it != rank_index.end()) {
  55. // Erase the book
  56. rank_index.erase(it);
  57.  
  58. // Adjust ranks for books with rank > the deleted rank
  59. for (auto it2 = rank_index.lower_bound(rank + 1); it2 != rank_index.end(); ++it2) {
  60. rank_index.modify(it2, [](Book& b) {
  61. --b.rank;
  62. });
  63. }
  64. }
  65. }
  66.  
  67. // Function to print all elements
  68. void print_all(const BookMultiIndex& books) {
  69. auto& rank_index = books.get<rank_tag>();
  70. std::cout << "Books in the container:" << std::endl;
  71. for (const auto& book : rank_index) {
  72. std::cout << "Rank = " << book.rank << ", Value = " << book.value << std::endl;
  73. }
  74. }
  75.  
  76. int main() {
  77. // Create the multi-index container
  78. BookMultiIndex books;
  79.  
  80. // Insert data into the container
  81. books.insert(Book(1, "a"));
  82. books.insert(Book(2, "b"));
  83. books.insert(Book(3, "c"));
  84. books.insert(Book(4, "d"));
  85.  
  86. // Print initial state
  87. print_all(books);
  88.  
  89. // Insert a new book at rank 2
  90. insert_book(books, 2, "f");
  91. std::cout << "\nAfter inserting at rank 2:" << std::endl;
  92. print_all(books);
  93.  
  94. // Delete the book at rank 2
  95. delete_book(books, 2);
  96. std::cout << "\nAfter deleting rank 2:" << std::endl;
  97. print_all(books);
  98.  
  99. return 0;
  100. }
  101.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
Books in the container:
Rank = 1, Value = a
Rank = 2, Value = b
Rank = 3, Value = c
Rank = 4, Value = d

After inserting at rank 2:
Books in the container:
Rank = 2, Value = f
Rank = 3, Value = a
Rank = 5, Value = d

After deleting rank 2:
Books in the container:
Rank = 2, Value = a
Rank = 4, Value = d