Python's enumerate in c++
In Python , instead of
colors = ['red', 'green', 'blue', 'yellow']
for i in range(len(colors)):
print i, '--->', colors[i]
One can write
for i, color in enumerate(colors):
print i, '--->', color
Is there a similar thing in c++?
c++ enumerator
add a comment |
In Python , instead of
colors = ['red', 'green', 'blue', 'yellow']
for i in range(len(colors)):
print i, '--->', colors[i]
One can write
for i, color in enumerate(colors):
print i, '--->', color
Is there a similar thing in c++?
c++ enumerator
I don't think so. If you iterate over astd::vector, it just gives you the elements, not the indexes.
– Barmar
Dec 23 '17 at 0:25
Possible duplicate of What is the most effective way to get the index of an iterator of an std::vector?
– Barmar
Dec 23 '17 at 0:27
add a comment |
In Python , instead of
colors = ['red', 'green', 'blue', 'yellow']
for i in range(len(colors)):
print i, '--->', colors[i]
One can write
for i, color in enumerate(colors):
print i, '--->', color
Is there a similar thing in c++?
c++ enumerator
In Python , instead of
colors = ['red', 'green', 'blue', 'yellow']
for i in range(len(colors)):
print i, '--->', colors[i]
One can write
for i, color in enumerate(colors):
print i, '--->', color
Is there a similar thing in c++?
c++ enumerator
c++ enumerator
asked Dec 23 '17 at 0:20
athosathos
2,18213062
2,18213062
I don't think so. If you iterate over astd::vector, it just gives you the elements, not the indexes.
– Barmar
Dec 23 '17 at 0:25
Possible duplicate of What is the most effective way to get the index of an iterator of an std::vector?
– Barmar
Dec 23 '17 at 0:27
add a comment |
I don't think so. If you iterate over astd::vector, it just gives you the elements, not the indexes.
– Barmar
Dec 23 '17 at 0:25
Possible duplicate of What is the most effective way to get the index of an iterator of an std::vector?
– Barmar
Dec 23 '17 at 0:27
I don't think so. If you iterate over a
std::vector, it just gives you the elements, not the indexes.– Barmar
Dec 23 '17 at 0:25
I don't think so. If you iterate over a
std::vector, it just gives you the elements, not the indexes.– Barmar
Dec 23 '17 at 0:25
Possible duplicate of What is the most effective way to get the index of an iterator of an std::vector?
– Barmar
Dec 23 '17 at 0:27
Possible duplicate of What is the most effective way to get the index of an iterator of an std::vector?
– Barmar
Dec 23 '17 at 0:27
add a comment |
3 Answers
3
active
oldest
votes
You can actually implement something similar in c++17.
Here is a sketch(c++-ish pseudocode), I use values everywhere and they should be replaced by appropriate references/forwarding, also you should fix how you get types (use iterator_traits), may be support unknown size, may be implement proper iterator interface etc
template <typename T>
struct EnumeratedIterator {
size_t index;
T iterator;
void operator++() {
++iterator;
}
std::pair<size_t, T>() {
return {index, *iterator};
}
bool operator !=(EnumeratedIterator o) {
return iterator != o.iterator;
}
}
template <typename T>
struct Enumerated {
T collection;
EnumeratedIterator<typename T::iterator> begin() {
return {0, collection.begin()};
}
EnumeratedIterator<typename T::iterator> end() {
return {collection.size(), collection.end()};
}
}
auto enumerate(T col) {
return Enumerated<T>(col);
}
and then use it like
for (auto [index, color] : enumerate(vector<int>{5, 7, 10})) {
assert(index < color);
}
add a comment |
Boost provides an adaptor which allows to do something similiar:
http://www.boost.org/doc/libs/1_63_0/libs/range/doc/html/range/reference/adaptors/reference/indexed.html
The following code is taken from the link above
#include <boost/range/adaptor/indexed.hpp>
#include <boost/assign.hpp>
#include <iterator>
#include <iostream>
#include <vector>
int main(int argc, const char* argv)
{
using namespace boost::assign;
using namespace boost::adaptors;
std::vector<int> input;
input += 10,20,30,40,50,60,70,80,90;
for (const auto& element : input | indexed(0))
{
std::cout << "Element = " << element.value()
<< " Index = " << element.index()
<< std::endl;
}
return 0;
}
3
Side topic, how doesinput += 10,20,30,40,50,60,70,80,90;work? I'm amazed...
– athos
Dec 23 '17 at 0:33
1
boost.org/doc/libs/1_62_0/libs/assign/doc/index.html#operator+=
– Omni
Dec 23 '17 at 0:39
add a comment |
Maybe you can emulate it like this:
int i = 0;
for (auto color : { "red", "green", "blue", "yellow" })
std::cout << i++ << "--->" << color << std::endl;
Can't setitoconstis my only complain...
– athos
Dec 23 '17 at 0:40
and now run this function two times and you'll get 4,5,6,7 instead of indexes
– RiaD
Dec 23 '17 at 0:44
@RiaD this is not a function
– Killzone Kid
Dec 23 '17 at 0:55
1
@KillzoneKid No, but this code must be put inside a function somewhere, and when it does, thestaticwill cause issues on repeated invocations of the function. Just remove thestatickeyword and move youriinitialisation to before the loop.
– hnefatl
Dec 23 '17 at 0:56
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47948726%2fpythons-enumerate-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can actually implement something similar in c++17.
Here is a sketch(c++-ish pseudocode), I use values everywhere and they should be replaced by appropriate references/forwarding, also you should fix how you get types (use iterator_traits), may be support unknown size, may be implement proper iterator interface etc
template <typename T>
struct EnumeratedIterator {
size_t index;
T iterator;
void operator++() {
++iterator;
}
std::pair<size_t, T>() {
return {index, *iterator};
}
bool operator !=(EnumeratedIterator o) {
return iterator != o.iterator;
}
}
template <typename T>
struct Enumerated {
T collection;
EnumeratedIterator<typename T::iterator> begin() {
return {0, collection.begin()};
}
EnumeratedIterator<typename T::iterator> end() {
return {collection.size(), collection.end()};
}
}
auto enumerate(T col) {
return Enumerated<T>(col);
}
and then use it like
for (auto [index, color] : enumerate(vector<int>{5, 7, 10})) {
assert(index < color);
}
add a comment |
You can actually implement something similar in c++17.
Here is a sketch(c++-ish pseudocode), I use values everywhere and they should be replaced by appropriate references/forwarding, also you should fix how you get types (use iterator_traits), may be support unknown size, may be implement proper iterator interface etc
template <typename T>
struct EnumeratedIterator {
size_t index;
T iterator;
void operator++() {
++iterator;
}
std::pair<size_t, T>() {
return {index, *iterator};
}
bool operator !=(EnumeratedIterator o) {
return iterator != o.iterator;
}
}
template <typename T>
struct Enumerated {
T collection;
EnumeratedIterator<typename T::iterator> begin() {
return {0, collection.begin()};
}
EnumeratedIterator<typename T::iterator> end() {
return {collection.size(), collection.end()};
}
}
auto enumerate(T col) {
return Enumerated<T>(col);
}
and then use it like
for (auto [index, color] : enumerate(vector<int>{5, 7, 10})) {
assert(index < color);
}
add a comment |
You can actually implement something similar in c++17.
Here is a sketch(c++-ish pseudocode), I use values everywhere and they should be replaced by appropriate references/forwarding, also you should fix how you get types (use iterator_traits), may be support unknown size, may be implement proper iterator interface etc
template <typename T>
struct EnumeratedIterator {
size_t index;
T iterator;
void operator++() {
++iterator;
}
std::pair<size_t, T>() {
return {index, *iterator};
}
bool operator !=(EnumeratedIterator o) {
return iterator != o.iterator;
}
}
template <typename T>
struct Enumerated {
T collection;
EnumeratedIterator<typename T::iterator> begin() {
return {0, collection.begin()};
}
EnumeratedIterator<typename T::iterator> end() {
return {collection.size(), collection.end()};
}
}
auto enumerate(T col) {
return Enumerated<T>(col);
}
and then use it like
for (auto [index, color] : enumerate(vector<int>{5, 7, 10})) {
assert(index < color);
}
You can actually implement something similar in c++17.
Here is a sketch(c++-ish pseudocode), I use values everywhere and they should be replaced by appropriate references/forwarding, also you should fix how you get types (use iterator_traits), may be support unknown size, may be implement proper iterator interface etc
template <typename T>
struct EnumeratedIterator {
size_t index;
T iterator;
void operator++() {
++iterator;
}
std::pair<size_t, T>() {
return {index, *iterator};
}
bool operator !=(EnumeratedIterator o) {
return iterator != o.iterator;
}
}
template <typename T>
struct Enumerated {
T collection;
EnumeratedIterator<typename T::iterator> begin() {
return {0, collection.begin()};
}
EnumeratedIterator<typename T::iterator> end() {
return {collection.size(), collection.end()};
}
}
auto enumerate(T col) {
return Enumerated<T>(col);
}
and then use it like
for (auto [index, color] : enumerate(vector<int>{5, 7, 10})) {
assert(index < color);
}
edited Nov 25 '18 at 6:06
answered Dec 23 '17 at 0:51
RiaDRiaD
32.8k955102
32.8k955102
add a comment |
add a comment |
Boost provides an adaptor which allows to do something similiar:
http://www.boost.org/doc/libs/1_63_0/libs/range/doc/html/range/reference/adaptors/reference/indexed.html
The following code is taken from the link above
#include <boost/range/adaptor/indexed.hpp>
#include <boost/assign.hpp>
#include <iterator>
#include <iostream>
#include <vector>
int main(int argc, const char* argv)
{
using namespace boost::assign;
using namespace boost::adaptors;
std::vector<int> input;
input += 10,20,30,40,50,60,70,80,90;
for (const auto& element : input | indexed(0))
{
std::cout << "Element = " << element.value()
<< " Index = " << element.index()
<< std::endl;
}
return 0;
}
3
Side topic, how doesinput += 10,20,30,40,50,60,70,80,90;work? I'm amazed...
– athos
Dec 23 '17 at 0:33
1
boost.org/doc/libs/1_62_0/libs/assign/doc/index.html#operator+=
– Omni
Dec 23 '17 at 0:39
add a comment |
Boost provides an adaptor which allows to do something similiar:
http://www.boost.org/doc/libs/1_63_0/libs/range/doc/html/range/reference/adaptors/reference/indexed.html
The following code is taken from the link above
#include <boost/range/adaptor/indexed.hpp>
#include <boost/assign.hpp>
#include <iterator>
#include <iostream>
#include <vector>
int main(int argc, const char* argv)
{
using namespace boost::assign;
using namespace boost::adaptors;
std::vector<int> input;
input += 10,20,30,40,50,60,70,80,90;
for (const auto& element : input | indexed(0))
{
std::cout << "Element = " << element.value()
<< " Index = " << element.index()
<< std::endl;
}
return 0;
}
3
Side topic, how doesinput += 10,20,30,40,50,60,70,80,90;work? I'm amazed...
– athos
Dec 23 '17 at 0:33
1
boost.org/doc/libs/1_62_0/libs/assign/doc/index.html#operator+=
– Omni
Dec 23 '17 at 0:39
add a comment |
Boost provides an adaptor which allows to do something similiar:
http://www.boost.org/doc/libs/1_63_0/libs/range/doc/html/range/reference/adaptors/reference/indexed.html
The following code is taken from the link above
#include <boost/range/adaptor/indexed.hpp>
#include <boost/assign.hpp>
#include <iterator>
#include <iostream>
#include <vector>
int main(int argc, const char* argv)
{
using namespace boost::assign;
using namespace boost::adaptors;
std::vector<int> input;
input += 10,20,30,40,50,60,70,80,90;
for (const auto& element : input | indexed(0))
{
std::cout << "Element = " << element.value()
<< " Index = " << element.index()
<< std::endl;
}
return 0;
}
Boost provides an adaptor which allows to do something similiar:
http://www.boost.org/doc/libs/1_63_0/libs/range/doc/html/range/reference/adaptors/reference/indexed.html
The following code is taken from the link above
#include <boost/range/adaptor/indexed.hpp>
#include <boost/assign.hpp>
#include <iterator>
#include <iostream>
#include <vector>
int main(int argc, const char* argv)
{
using namespace boost::assign;
using namespace boost::adaptors;
std::vector<int> input;
input += 10,20,30,40,50,60,70,80,90;
for (const auto& element : input | indexed(0))
{
std::cout << "Element = " << element.value()
<< " Index = " << element.index()
<< std::endl;
}
return 0;
}
edited Dec 23 '17 at 0:37
answered Dec 23 '17 at 0:28
OmniOmni
716411
716411
3
Side topic, how doesinput += 10,20,30,40,50,60,70,80,90;work? I'm amazed...
– athos
Dec 23 '17 at 0:33
1
boost.org/doc/libs/1_62_0/libs/assign/doc/index.html#operator+=
– Omni
Dec 23 '17 at 0:39
add a comment |
3
Side topic, how doesinput += 10,20,30,40,50,60,70,80,90;work? I'm amazed...
– athos
Dec 23 '17 at 0:33
1
boost.org/doc/libs/1_62_0/libs/assign/doc/index.html#operator+=
– Omni
Dec 23 '17 at 0:39
3
3
Side topic, how does
input += 10,20,30,40,50,60,70,80,90; work? I'm amazed...– athos
Dec 23 '17 at 0:33
Side topic, how does
input += 10,20,30,40,50,60,70,80,90; work? I'm amazed...– athos
Dec 23 '17 at 0:33
1
1
boost.org/doc/libs/1_62_0/libs/assign/doc/index.html#operator+=
– Omni
Dec 23 '17 at 0:39
boost.org/doc/libs/1_62_0/libs/assign/doc/index.html#operator+=
– Omni
Dec 23 '17 at 0:39
add a comment |
Maybe you can emulate it like this:
int i = 0;
for (auto color : { "red", "green", "blue", "yellow" })
std::cout << i++ << "--->" << color << std::endl;
Can't setitoconstis my only complain...
– athos
Dec 23 '17 at 0:40
and now run this function two times and you'll get 4,5,6,7 instead of indexes
– RiaD
Dec 23 '17 at 0:44
@RiaD this is not a function
– Killzone Kid
Dec 23 '17 at 0:55
1
@KillzoneKid No, but this code must be put inside a function somewhere, and when it does, thestaticwill cause issues on repeated invocations of the function. Just remove thestatickeyword and move youriinitialisation to before the loop.
– hnefatl
Dec 23 '17 at 0:56
add a comment |
Maybe you can emulate it like this:
int i = 0;
for (auto color : { "red", "green", "blue", "yellow" })
std::cout << i++ << "--->" << color << std::endl;
Can't setitoconstis my only complain...
– athos
Dec 23 '17 at 0:40
and now run this function two times and you'll get 4,5,6,7 instead of indexes
– RiaD
Dec 23 '17 at 0:44
@RiaD this is not a function
– Killzone Kid
Dec 23 '17 at 0:55
1
@KillzoneKid No, but this code must be put inside a function somewhere, and when it does, thestaticwill cause issues on repeated invocations of the function. Just remove thestatickeyword and move youriinitialisation to before the loop.
– hnefatl
Dec 23 '17 at 0:56
add a comment |
Maybe you can emulate it like this:
int i = 0;
for (auto color : { "red", "green", "blue", "yellow" })
std::cout << i++ << "--->" << color << std::endl;
Maybe you can emulate it like this:
int i = 0;
for (auto color : { "red", "green", "blue", "yellow" })
std::cout << i++ << "--->" << color << std::endl;
edited Dec 23 '17 at 1:10
answered Dec 23 '17 at 0:38
Killzone KidKillzone Kid
4,8453728
4,8453728
Can't setitoconstis my only complain...
– athos
Dec 23 '17 at 0:40
and now run this function two times and you'll get 4,5,6,7 instead of indexes
– RiaD
Dec 23 '17 at 0:44
@RiaD this is not a function
– Killzone Kid
Dec 23 '17 at 0:55
1
@KillzoneKid No, but this code must be put inside a function somewhere, and when it does, thestaticwill cause issues on repeated invocations of the function. Just remove thestatickeyword and move youriinitialisation to before the loop.
– hnefatl
Dec 23 '17 at 0:56
add a comment |
Can't setitoconstis my only complain...
– athos
Dec 23 '17 at 0:40
and now run this function two times and you'll get 4,5,6,7 instead of indexes
– RiaD
Dec 23 '17 at 0:44
@RiaD this is not a function
– Killzone Kid
Dec 23 '17 at 0:55
1
@KillzoneKid No, but this code must be put inside a function somewhere, and when it does, thestaticwill cause issues on repeated invocations of the function. Just remove thestatickeyword and move youriinitialisation to before the loop.
– hnefatl
Dec 23 '17 at 0:56
Can't set
i to const is my only complain...– athos
Dec 23 '17 at 0:40
Can't set
i to const is my only complain...– athos
Dec 23 '17 at 0:40
and now run this function two times and you'll get 4,5,6,7 instead of indexes
– RiaD
Dec 23 '17 at 0:44
and now run this function two times and you'll get 4,5,6,7 instead of indexes
– RiaD
Dec 23 '17 at 0:44
@RiaD this is not a function
– Killzone Kid
Dec 23 '17 at 0:55
@RiaD this is not a function
– Killzone Kid
Dec 23 '17 at 0:55
1
1
@KillzoneKid No, but this code must be put inside a function somewhere, and when it does, the
static will cause issues on repeated invocations of the function. Just remove the static keyword and move your i initialisation to before the loop.– hnefatl
Dec 23 '17 at 0:56
@KillzoneKid No, but this code must be put inside a function somewhere, and when it does, the
static will cause issues on repeated invocations of the function. Just remove the static keyword and move your i initialisation to before the loop.– hnefatl
Dec 23 '17 at 0:56
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47948726%2fpythons-enumerate-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I don't think so. If you iterate over a
std::vector, it just gives you the elements, not the indexes.– Barmar
Dec 23 '17 at 0:25
Possible duplicate of What is the most effective way to get the index of an iterator of an std::vector?
– Barmar
Dec 23 '17 at 0:27