Collection of C++ snippets
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

1.3 KiB


for(auto x : counted(v)) {
	... x.value ... x.index ...

x.value is a reference to the element in the container (so you can change it's value), x.index is what it is. Container should have forward iterators.

This headers is purely a handy tool for the range-based-for-loops (ie for(auto x : counter(v))). Using it explicitly with iterators is not recommended!

There is no const version of it. Doing for(const auto x : counted(v)) doesn't make it impossible to change x (so the element in the container can be modified).


for(auto x : counted(v)) {
	std::cout << "v[" << x.index << "] = " << x.value << std::endl;
	x.value *= 2;

This will output the container v, with it's indeces. And it will multiply every value in v by 2.

tuple element

map<.., ..> m = {..};
for(auto key : keys(m))
	... key ...
for(auto v : values(m))
	... v ...

Some helper functions for easy access to maps. It also works well on vectors of tuples, or any container with a tuple-like interface. The general function is nth_values<N>(v), which allows you to iterate over the N-th element in the tuples of v. For more information see tuple_element.h. Note that this does have a good const version, in contrast to the thing above (counted).