有没有标准的 C++ 用于分解 std::pair 的函数对象?

发布于 2024-07-11 04:14:28 字数 216 浏览 6 评论 0原文

有谁知道是否存在用于访问 std::pair 元素的事实上的标准(即 TR1 或 Boost)C++ 函数对象? 在过去的 24 小时内,我曾两次希望拥有类似于 Perl 哈希的 keys 函数的东西。 例如,最好在 std::map 对象上运行 std::transform 并将所有键(或值)转储到另一个容器。 我当然可以编写这样的函数对象,但我更愿意重用那些吸引了很多眼球的东西。

Does anyone know if there's a de-facto standard (i.e., TR1 or Boost) C++ function object for accessing the elements of a std::pair? Twice in the past 24 hours I've wished I had something like the keys function for Perl hashes. For example, it would be nice to run std::transform on a std::map object and dump all the keys (or values) to another container. I could certainly write such a function object but I'd prefer to reuse something that's had a lot of eyeballs on it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

无悔心 2024-07-18 04:14:29

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::bind is what you look for.

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

Example:

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));
如梦初醒的夏天 2024-07-18 04:14:29

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

From the way you worded your question, I'm not sure this is a proper response, but try boost::tie (part of the Boost::tuple library). It works on std::pairs too.

最后的乘客 2024-07-18 04:14:29

boost::bind 通常用于适应 std ::映射容器以与算法一起使用。 这里是一个示例:

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


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)));

boost::bind is often used to adapt std::map containers for use with algorithms. Here is an example:

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


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)));
往事随风而去 2024-07-18 04:14:29

使用不同容器的组合怎么样?

例如,当我想将向量划分为补充映射中包含的项目和补充映射中未包含的项目时,我使用了以下内容:

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()));

现在我遇到了第二个问题 - 在应用程序运行期间, user_info 的 status bool 可以更改,稍后我想使用状态 bool 为 true 的项目重新分区向量,而不仅仅是包含在补充地图中。

但是,我似乎在访问嵌套对的第二项时遇到问题。

我尝试了以下操作,但我似乎无法访问嵌套对!

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); 

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); 
夏尔 2024-07-18 04:14:29

看一下 boost::adaptors。 有预定义的适配器用于迭代映射键或值,而无需将它们复制到中间容器。

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

め可乐爱微笑 2024-07-18 04:14:29

没有建议的一个选项是 std::tr1::get。 请参阅 n1745 的第 6.1.2 和 6.1.4 节

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

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

绝对不如您提到的 map 提取案例中的 bind 那么容易使用,但仍然值得了解。 改编约翰内斯的代码:

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文