Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Using modern C++ and templates, provides a function `enumerate` that easily returns key-value pairs as seen in the Python programming language.

License

NotificationsYou must be signed in to change notification settings

TheMaverickProgrammer/C-Python-like-Iterators

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Header only and using modernC++ 17 and templates, provides a functionenumerate that easily returns key-value pairs for any standard container type as seen in the Python programming language.

Tested on x86-64 gcc

Notes on index and key-type deduction

Index values are alwayssize_t. If you want the index, use that.For map types that have a key, the resolved variable type needs to be that type.

See the comments in the code sample below

Minimal Working Example

#include "Enumerate.h"#include <iostream>#include <vector>#include <map>#include <string>int main(int argc, char** argv) {    std::vector<int> myVec({10,9,8,7,6,5,4,3,2,1});    // Capture index and value    for(const auto& [i, n] : enumerate(myVec)){        std::cout << "index: " << i << " value: " << n << std::endl;                // n is mutable        ++n; // will increase all values in the list after printing    }    std::map<std::string, int> map{{"a", 10}, {"b", 9},{"c", 7}, {"d", 8}};        // Capture just the index    for(size_t index : enumerate(map)) {        // index for key-value pair as if it was sequential        std::cout << "index: " << index << std::endl;    }        // Capture just the key    for(std::string key : enumerate(map)) {        // the resolved value type has to be the same as the map's key type        std::cout << "key: " << key << std::endl;    }        // Capture the index and the items    for(const auto& [index, item] : enumerate(map)) {        // Decompose the item into keys and values        const auto& [key, value] = item;        std::cout << "index: " << index << " key: " << key << std::endl;    }}

About

Using modern C++ and templates, provides a function `enumerate` that easily returns key-value pairs as seen in the Python programming language.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp