This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename Iterator, typename Distance> | |
void advance(BidirectionalIterator<Iterator>& it, Distance n) | |
{ | |
// Bidirectional implementation | |
} | |
template<typename Iterator, typename Distance> | |
void advance(RandomAccessIterator<Iterator>& it, Distance n) | |
{ | |
// Random access implementation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct LinkedListIterator | |
{ | |
// No tag here | |
}; | |
struct VectorIterator | |
{ | |
// No tag here | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct LinkedListIterator | |
{ | |
using iterator_category = std::bidirectional_iterator_tag; | |
}; | |
struct VectorIterator | |
{ | |
using iterator_category = std::random_access_iterator_tag; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename Iterator, typename Distance> | |
void advance(Iterator& it, Distance n, std::bidirectional_iterator_tag) | |
{ | |
// Bidirectional implementation | |
} | |
template<typename Iterator, typename Distance> | |
void advance(Iterator& it, Distance n, std::random_access_iterator_tag) | |
{ | |
// Random access implementation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// O(n) complexity | |
std::advance(linkedList.begin(), n); | |
// O(1) complexity (taking advantage of random access iterator) | |
std::advance(vector.begin(), n); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass | |
{ | |
public: | |
enum ContructionWay {ConstructThisWay, ConstructThatWay}; | |
explicit MyClass(ContructionWay); | |
// ... | |
}; | |
MyClass x(MyClass::ConstructThisWay); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::vector<MyClass> xs; | |
std::generate_n(std::back_inserter(xs), n, MyClass::constructThisWay); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass | |
{ | |
public: | |
static MyClass constructThisWay(); | |
static MyClass constructThatWay(); | |
// ... | |
}; | |
auto x = MyClass::constructThatWay(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass | |
{ | |
public: | |
static struct ConstructThisWay{} constructThisWay; | |
static struct ConstructThatWay{} constructThatWay; | |
explicit MyClass(ConstructThisWay); | |
explicit MyClass(ConstructThatWay); | |
// ... | |
}; |