logo
down
shadow

Iterator for jumping between container C++


Iterator for jumping between container C++

By : user2948035
Date : November 14 2020, 04:48 PM
I think the issue was by ths following , Let's focus on a const iterator. Such an iterator should hold an iterator to each of the deques, but also to each of their end (why you need this is explained below). But these iterators should be held in a vector instead of a deque, because you will never push or pop from either end of these containers, since the lifetime of such an iterator ends with a modification of your data structure.
So basically, you need the following structure:
code :
struct const_iterator {
    vector<deque<int>::const_iterator>   iterators;
    vector<deque<int>::const_iterator>   ends;
    //...
};
struct nested_deque_iterator {
    vector<deque<int>::const_iterator>            iterators;
    vector<deque<int>::const_iterator>            ends;
    vector<deque<int>::const_iterator>::iterator  current;
    bool                                          at_end = false;
};
void update_current() {
    if (!at_end && iterators.size() > 0) {
        // At the beginning, we assume that we didn't find any
        // new "next smaller element" in any deque.
        at_end = true;

        // Iterate through the deques (with two iterators in parallel)
        for (auto i = iterators.begin(), e = ends.begin();
             i != iterators.end() && e != ends.end();
             ++i, ++e)
        {
            // We ignore deques which are already at their end
            if (i != e) {
                // If we found a smaller next element (or the first try)...
                if (at_end || *i < *next) {
                    // ... then replace the current iterator with it:
                    at_end = false;
                    current = i;
                }
            }
        }
    }
}
int operator *() const {
    return **current;
}
nested_deque_iterator& operator++() {
    if (!at_end) {
        ++(*current);
        update_current();
    }
}
nested_deque_iterator operator++(int) {
    nested_deque_iterator old(*this);
    ++(*this);
    return old;
}
bool operator==(const nested_deque_iterator &o) const {
    // If either iterator is at the end, don't dereference current!
    if (at_end || o.at_end) {
        return at_end == o.at_end;
    }
    return *current == *(o.current);
}
bool operator!=(const nested_deque_iterator &o) const {
    return !(*this == o);
}
nested_deque_iterator nested_deque_begin(const deque<deque<int>> & deques) {
    vector<deque<int>::const_iterator>   iterators;
    vector<deque<int>::const_iterator>   ends;
    for (const auto & d : deques) {
        iterators.push_back(d.begin());
        ends.push_back(d.end());
    }
    return { iterators, ends };
}

nested_deque_iterator nested_deque_end(const deque<deque<int>> & deques) {
    vector<deque<int>::const_iterator>   ends;
    for (const auto & d : deques) {
        ends.push_back(d.end());
    }
    return { ends, ends };
}
struct nested_deque {
    deque<deque<int>> deques;
    //...
}

nested_deque_iterator begin(const nested_deque & nd) {
    return nested_deque_begin(nd.deques);
}
nested_deque_iterator end(const nested_deque & nd) {
    return nested_deque_end(nd.deques);
}
deque<deque<int>> mydeque = ...;

for (int i : nested_deque{mydeque}) {
    // Here you go.
}


Share : facebook icon twitter icon
What happens if you increment an iterator that is equal to the end iterator of an STL container

What happens if you increment an iterator that is equal to the end iterator of an STL container


By : Omar Osorio
Date : March 29 2020, 07:55 AM
should help you out What if I increment an iterator by 2 when it points onto the last element of a vector? In this question asking how to adjust the iterator to an STL container by 2 elements two different approaches are offered: , Following is the quote from Nicolai Josuttis book:
C++, error when using iterator for a map container that is pointed to. map/set iterator not incrementable

C++, error when using iterator for a map container that is pointed to. map/set iterator not incrementable


By : Vincent Danti
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , The removePiece() function removes the element that it is referring to, invalidating it. An attempt is then made to increment it resulting in the assertion failure. From map::erase():
code :
newBoard->pieces.clear();
Is it safe for the input iterator and output iterator in std::transform to be from the same container?

Is it safe for the input iterator and output iterator in std::transform to be from the same container?


By : Nunu Nugraha
Date : March 29 2020, 07:55 AM
will help you Yes, this is guaranteed to be safe (as long as operation itself doesn't modify the elements or invalidate iterators).
From chapter [alg.transform] from draft n3337:
code :
template<class InputIterator, class OutputIterator,  
    class UnaryOperation>  
OutputIterator  
transform(InputIterator first, InputIterator last,  
    OutputIterator result, UnaryOperation op);  

template<class InputIterator1, class InputIterator2,  
    class OutputIterator, class BinaryOperation>  
OutputIterator  
transform(InputIterator1 first1, InputIterator1 last1,  
    InputIterator2 first2, OutputIterator result,  
    BinaryOperation binary_op);  
Return an iterator to boost multiintex container as a std::set<>::iterator

Return an iterator to boost multiintex container as a std::set<>::iterator


By : dewhite04
Date : November 18 2020, 11:01 PM
With these it helps No, you can't. The EmployeeSet you have defined works like a std::set (a std::multiset, actually), but it is not one. The iterator types of these unrelated containers are different and you can't pass one as the other.
Maybe you can reconsider why you need to pass an iterator to an index of a multi_index_container as a std::set::iterator.
How to create iterator for member element from the iterator of std container?

How to create iterator for member element from the iterator of std container?


By : user2285225
Date : March 29 2020, 07:55 AM
like below fixes the issue With range-v3, you may create view:
code :
std::vector<A> mycoll = {{10,'a'}, {20,'b'}, {30,'c'} };

for (auto e : mycoll | ranges::view::transform(&A::x)) {
    std::cout << e << " "; // 10 20 30
}
auto it = ranges::v3::lower_bound(mycoll, value, std::less<>{}, &A::x);
// return iterator of mycoll directly :-)
auto it = std::lower_bound(mycoll.begin(), mycoll.end(),
                           value,
                           [](const A& a, int x){ return a.x < x; });
Related Posts Related Posts :
  • Stuck when testing custom list class in c++
  • Using each member of class within one function to calculate average
  • check whether if two squares are intersecting with each other
  • Glm Quaternion lookat function
  • Is there guarantee heap allocated block address will not change(implicitly)?
  • Cosine Calculation without cmath library
  • Manually deleting a pointer returned by function
  • Avoid output interleaving
  • C++ error : Expected an identifier
  • Segmentation fault when I call operator new in linux mint
  • Recursively Solving A Sudoku Puzzle Using Backtracking Theoretically
  • lambda closure type and default argument in function template
  • How to ensure the function return consistent floating point values in C/C++?
  • What does throw do when not in used with try and catch?
  • The Unpredictablilty of the Order of Evaluation of Subexpressions
  • cannot convert const wchar_t to 'const_char' for argument 2 in MessageBOX etc etc
  • Size of a empty Class & Derived Virtual Class
  • unformatted i/o to and from memory
  • Storing an std::thread object as a class member
  • Remove duplicate string in an array
  • How to use copy_if() polymorphically?
  • How to organize a class's member functions?
  • How to wrap std::chrono in a c++ iterator type
  • Check if a socket is connected in mac os x
  • Grabbing the mouse cursor in GLFW
  • Calling Windows shell menu (same as right-click in Explorer) for multiple files programmatically
  • Pop up dialog to save file in seperate thread
  • Why would you use the keyword const if you already know variable should be constant?
  • Detecting Small Sound Effects In C++
  • How to avoid the copy when I return
  • Getting error code C2228: left of '._Ptr' must have class/struct/union
  • In win32 C++ programming how can I duplicate a window style?
  • Should the order of import statements matter when importing a .so?
  • Design decision regarding std::array fill
  • Access array of C++ structs from Fortran?
  • Determining when functions allocate memory in C++
  • C++ using std::set remove duplicate item for a struct fail.
  • Code to find out the number of triplets which lead to a sum less than or equal to threshold is giving a wrong output
  • Converting glm::lookat matrix to quaternion and back
  • Searching multi-dimensional vectors
  • Send and receive via SOCKS5 c++
  • GDI+: unhandled exception when drawing bitmap
  • Editing other processes memory
  • Object deleted when its member function is being executed?
  • Infinite Loop while inputting the different data type values in STACK
  • const members and operator=
  • Threaded Video Player sync
  • Does infinite of floating point number satisfy these equation?
  • map sorting in c++ by the frequencies
  • The Preprocessor program linked in g++ similar to the cpp program in gcc
  • How is the union used in this OpenCV library?
  • C++ memory leak in recursion
  • C++ Error: C4430 and C2143 Error at an impossible place
  • How can I track object lifetime in C++11 lambda?
  • #include statement mapping in Biicode (biicode.conf)
  • std::equal gives "Term doesnt evaluate to a function taking 2 arguments"
  • C++ template argument as reference lvalue
  • Legal to forward declare C standard library entities but not C++ standard library entities?
  • Conversion of wchar_t* to string
  • VirtualTreeView - Embarcadero C++ Builder XE7 - 64 bits
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org