English 中文(简体)
是否有一个标准的C++函数对象可以拆解一个std::pair?
原标题:
  • 时间:2008-12-16 21:01:42
  •  标签:

有人知道是否有一种事实上的标准(例如TR1或Boost)C++函数对象用于访问std::pair的元素吗?在过去的24小时内,我已经两次希望有类似Perl哈希的keys函数的东西。例如,对于一个std::map对象,运行std::transform并将所有键(或值)转储到另一个容器中会很不错。我肯定可以编写这样一个函数对象,但我更愿意重用已经经过多次检查的东西。

最佳回答

boost::bind是您要寻找的东西。

boost::bind(&std::pair::second, _1); // returns the value of a pair

例子

typedef std::map<std::string, int> map_type;

std::vector<int> values; // will contain all values
map_type map;
std::transform(map.begin(), 
               map.end(), 
               std::back_inserter(values), 
               boost::bind(&map_type::value_type::second, _1));
问题回答

从您提出问题的方式来看,我不确定这是否是一个适当的回答,但是可以尝试使用boost::tie(Boost::tuple库的一部分)。它也可以用于std::pair

boost :: bind通常用于适应std :: map容器以在算法中使用。 这里是一个例子。

void print_string(const std::string& s) {
  std::cout << s <<  
 ;
}


std::map<int,std::string> my_map;
my_map[0]="Boost";
my_map[1]="Bind";


std::for_each(my_map.begin(), my_map.end(),
              boost::bind(&print_string, boost::bind(
              &std::map<int,std::string>::value_type::second,_1)));

What about using combinations of different containers.

For example when I wanted to partition a vector into items contained in a supplemental map and items that where not contained in the supplemental map I used the following:

typedef int DWORD; 
typedef std::pair<std::string, bool> user_info; 
typedef std::map<DWORD, user_info> USER_MAP; 
typedef std::vector<DWORD> VEC_STAFF; 

VEC_STAFF::iterator it = std::partition(Staff.begin(), Staff.end(), (bind(&USER_MAP::find, m_Users, _1) != m_Users.end()));

Now I have a second problem - during the running of the application the status bool of user_info can change, and later on I want to re-partition the vector with items that have a status bool of true rather than just being contained in the supplemental map.

However I seem to have a problem accessing the second item of a nested pair.

I tried the following but I cannot seem to access the nested pair!

CActiveUsers::VEC_STAFF::const_iterator itCurEnd = partition(Staff.begin(), Staff.end(), bind(&USER_MAP::value_type::second::second, bind(&USER_MAP::find, &m_Users, _1)) == true); 

Take a look at boost::adaptors. There are predefined adaptors for iterating over map keys or values without copying them to an intermediate container.

One option that wasn t suggested is std::tr1::get. See sections 6.1.2 and 6.1.4 of n1745.

std::pair< std::string, int > p( "foo", 1729 );

int hr = std::tr1::get< 1 >( p );

Definitely not as easy to use as bind in the map extraction case you mentioned but still worth knowing about. Adapting Johannes code:

typedef std::map<std::string, int> map_type;

std::vector<int> values; // will contain all values
map_type map;

// std::tr1::get is overloaded so we need to help the compiler choose
const map_type::value_type::second_type & (*get)( const map_type::value_type & ) =
  &std::tr1::get< 1, map_type::value_type::first_type, map_type::value_type::second_type >;

std::transform(map.begin(), 
               map.end(), 
               std::back_inserter(values), 
               get);




相关问题
热门标签