Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

c++ - Access index of current element in range-for loop c++20

In a pre-c++20 question Access index in range-for loop (answer), it was mentioned that

2020 note: It would probably be more wise to use a lambda-based solution instead of macro trickery.

Could someone post the lambda-based solution for c++20? Are there any other solutions new to c++20?


Edit: I have a main loop which is polymorphic over the size of a vector. Since I have to read and modify this piece of code frequently, I think I'm justified wanting the nicest solution. What happens if I want to use views::reverse sometimes in the main loop and not others? (I don't actually need this, and solved my immediate problem after refactoring, but still I'm using additional initializations so frequently to access the current element in cold development areas of my code base, it's worth finding a solution)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After looking into defining a view interface, I decided to use range-v3.

There are several things missing from std::ranges https://stackoverflow.com/a/68172842/11998382, and until they are added in future standards, it is sensible to use range-v3 rather than repeatedly attempting non-trivial implementations yourself.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...