C++中标准模板库std::pair的实现

        以下用C++实现了标准模板库中的std::pair实现,参考了 cplusplus 和 vs2013中的utility文件。

        关于std::pair的介绍和用法可以参考: https://blog.csdn.net/fengbingchun/article/details/52205149 

        实现代码pair.hpp如下:

#ifndef FBC_STL_PAIR_HPP_
#define FBC_STL_PAIR_HPP_

namespace fbcstd {

template
struct pair {
	typedef T1 first_type;
	typedef T2 second_type;

	// default construct
	pair() : first(), second() {}
	// construct from compatible pair
	template
	pair(const pair& pr) : first(pr.first), second(pr.second) {}
	// construct from specified values
	pair(const first_type& a, const second_type& b) : first(a), second(b) {}

	// assign from copied pair
	pair& operator = (const pair& pr) 
	{
		first = pr.first;
		second = pr.second;

		return (*this);
	}

	T1 first;
	T2 second;
}; // struct pair

// relational operators for pair
template
inline bool operator == (const pair& lhs, const pair& rhs)
{
	return (lhs.first == rhs.first && lhs.second == rhs.second);
}

template
inline bool operator != (const pair& lhs, const pair& rhs)
{
	return (!(lhs == rhs));
}

template
inline bool operator < (const pair& lhs, const pair& rhs)
{
	return (lhs.first < rhs.first || (!(rhs.first < lhs.first) && lhs.second < rhs.second));
}

template
inline bool operator > (const pair& lhs, const pair& rhs)
{
	return (rhs < lhs);
}

template
inline bool operator <= (const pair& lhs, const pair& rhs)
{
	return (!(rhs < lhs));
}

template
inline bool operator >= (const pair& lhs, const pair& rhs)
{
	return (!(lhs < rhs));
}

} // namespace fbcstd

#endif // FBC_STL_PAIR_HPP_

        测试代码test_pair.cpp如下:

#include "pair.hpp"
#include 

#define std fbcstd

int main()
{
	std::pair foo;
	std::pair bar(10, 20);
	std::pair car(bar);
	fprintf(stdout, "foo: %d, %d; bar: %d, %d; car: %d, %d\n", foo.first, foo.second, bar.first, bar.second, car.first, car.second);
	
	foo = car;
	fprintf(stdout, "foo: %d, %d\n", foo.first, foo.second);

	if (foo == bar) fprintf(stdout, "foo == bar\n");
	else fprintf(stdout, "foo != bar\n");

	std::pair cat(10, 15);
	
	if (cat > bar) fprintf(stdout, "cat > bar\n");
	else if (cat == bar) fprintf(stdout, "cat == bar\n");
	else if (cat < bar) fprintf(stdout, "cat < bar\n");

	bar.first = -10;
	bar.second = -20;
	fprintf(stdout, "bar: %d, %d\n", bar.first, bar.second);

	fprintf(stdout, "test pair finish!\n");
	return 0;
}

        CMakeLists.txt内容如下:

PROJECT(Samples_STL_Implementation)
CMAKE_MINIMUM_REQUIRED(VERSION 3.0)

SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -O2 -std=c11")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -g -Wall -O2 -std=c++11")

INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/src)
MESSAGE(STATUS "project source dir: ${PROJECT_SOURCE_DIR}")

FILE(GLOB tests ${PROJECT_SOURCE_DIR}/test/*.cpp)

FOREACH (test ${tests})
	STRING(REGEX MATCH "[^/]+$" test_file ${test})
	STRING(REPLACE ".cpp" "" test_basename ${test_file})
	ADD_EXECUTABLE(${test_basename} ${test})
	TARGET_LINK_LIBRARIES(${test_basename} pthread)
ENDFOREACH()

        脚本build.sh内容如下:

#! /bin/bash

echo "Note: new create build directory, and executable file in build"
echo ${PWD}
mkdir -p build
cd build
cmake ..
make

        编译及测试方法:将终端定位到Samples_STL_Implementation目录下,执行:$ ./build.sh , 然后再执行$ ./build/test_pair即可。

        GitHub:  https://github.com/fengbingchun/Linux_Code_Test


你可能感兴趣的:(C/C++/C++11)