C++ foreach implementation

C++ foreach

Note: I am original, if you find similarities, you will be responsible for the consequences

C++ 11

C++ 11 is directly supported, __cpp_range_based_forthe value of judgment , if it is, it 200907Lis supported

#include <iostream>
#include <vector>

int main(void)
{
    
    
	std::vector<int> vec{
    
    1,2,3,4,5};
	
#if __cpp_range_based_for >= 200907L
	for (int var : vec)
	{
    
    
		std::cout << var << std::endl;
	}
#endif
}

Output result

1
2
3
4
5

Before C++ 11

Only one class can be written to control the
file name:foreach.hpp

// foreach.hpp
#ifndef FOREACH_HPP
#define FOREACH_HPP

template <typename container_type>
class Foreach_container
{
    
    
public:

    Foreach_container(const container_type &c) : begin(c.begin()), end(c.end()) {
    
    }
    Foreach_container(container_type &c) : begin(c.begin()), end(c.end()) {
    
    }

    decltype(container_type().begin()) begin, end;
    bool f = true;
};

#define FOREACH(var, container)                                                \
for (auto _container_ = in::Foreach_container<decltype(container)>(container); \
    _container_.f && _container_.begin != _container_.end;                     \
    ++_container_.begin, _container_.f = true)                                 \
                                                                               \
    for (var = *_container_.begin; _container_.f; _container_.f = false)

#ifndef foreach
# define foreach FOREACH
#endif

#endif // FOREACH_HPP

Instructions

#include <iostream>
#include <vector>
#include "foreach.hpp"

int main(void)
{
    
    
	std::vector<int> vec{
    
    1,2,3,4,5};
	
	// 使用foreach宏
	foreach (int var, vec)
	{
    
    
		std::cout << var << std::endl;
	}
}

Output result

1
2
3
4
5

Combine

#include <iostream>
#include <vector>

#if __cpp_range_based_for >= 200907L

	#define FOREACH(var, container) for (var : container)
	
#else
	template <typename container_type>
	class Foreach_container
	{
    
    
	public:
	
	    Foreach_container(const container_type &c) : begin(c.begin()), end(c.end()) {
    
    }
	    Foreach_container(container_type &c) : begin(c.begin()), end(c.end()) {
    
    }
	
	    decltype(container_type().begin()) begin, end;
	    bool f = true;
	};
	
	#define FOREACH(var, container)                                                \
	for (auto _container_ = in::Foreach_container<decltype(container)>(container); \
	    _container_.f && _container_.begin != _container_.end;                     \
	    ++_container_.begin, _container_.f = true)                                 \
	                                                                               \
	    for (var = *_container_.begin; _container_.f; _container_.f = false)
#endif

#ifndef foreach
# define foreach FOREACH
#endif

int main(void)
{
    
    
	std::vector<int> vec{
    
    1,2,3,4,5};
	
	// 使用foreach宏
	foreach (int var, vec)
	{
    
    
		std::cout << var << std::endl;
	}
}

Guess you like

Origin blog.csdn.net/m0_47534090/article/details/108780357