mirror of
https://git.adityakumar.xyz/data-structures.git
synced 2024-11-13 22:49:44 +00:00
30 lines
1 KiB
C++
30 lines
1 KiB
C++
#include <iostream>
|
|
#include <forward_list>
|
|
#include <vector>
|
|
|
|
int main() {
|
|
std::vector<std::string> vec { "Lewis Hamilton", "Lewis Hamilton", "Nico Roseberg", "Sebastain Vettel", "Lewis Hamilton", "Sebastain Vettel", "Sebastain Vettel", "Sebastain Vettel", "Fernando Alonso" };
|
|
|
|
auto it { vec.begin() }; // Constant time
|
|
std::cout << "Latest winner is: " << *it << std::endl;
|
|
|
|
it += 8; // Constant time
|
|
std::cout << "Winner before 8 years was: " << *it << std::endl;
|
|
|
|
std::advance(it, -3); // Contant time
|
|
std::cout << "Winner before 3 years of that was: " << *it << std::endl;
|
|
|
|
std::forward_list<std::string> fwd { vec.begin(), vec.end() };
|
|
|
|
auto it1 { fwd.begin() };
|
|
std::cout << "Latest winner is: " << *it1 << std::endl;
|
|
|
|
std::advance(it1, 5); // Time taken is proportional to the number of elements
|
|
|
|
std::cout << "Winner before 5 years was: " << *it1 << std::endl;
|
|
|
|
// Going back will result in compilr time error as forward_list only allows us to move towards the end.
|
|
// std::advance(it1, -2); // Compiler error
|
|
|
|
return 0;
|
|
}
|