Есть ли стандартный функциональный объект C++ для того, чтобы демонтировать станд.:: пара?

Вы можете напечатать его, как показано ниже:

for k, v in data.items():
    print('[{key}, {values}]'.format(key=k, values=', '.join('[{}]'.format(', '.join(x.split())) for x in v)))

На выходе получается:

[MEXICO, [2006/10/20, 2.8], [2006/10/18, 3.3]]
[HAWAII, [2006/10/19, 2.5], [2006/10/20, 3.1]]
[MISSOURI, [2006/10/18, 3.4]]
[PANAMA, [2006/10/18, 5.0]]
[VANUATU, [2006/10/18, 6.2]]
[ALASKA, [2006/10/19, 2.8], [2006/10/18, 2.6], [2006/10/18, 2.7], [2006/10/18, 2.7], [2006/10/18, 2.8]]
[INDONESIA, [2006/10/20, 4.9]]
10
задан Soo Wei Tan 2 May 2012 в 20:58
поделиться

4 ответа

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));
17
ответ дан 3 December 2019 в 16:11
поделиться

От пути Вы сформулировали свой вопрос, я не уверен, что это - надлежащий ответ, но попытка boost::tie (часть Повышения:: библиотека кортежа). Это продолжает работать std::pairs также.

4
ответ дан 3 December 2019 в 16:11
поделиться

повышение:: свяжите часто используется для адаптации станд.:: контейнеры карты для использования с алгоритмами. Вот пример:

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)));
3
ответ дан 3 December 2019 в 16:11
поделиться

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); 
1
ответ дан 3 December 2019 в 16:11
поделиться
Другие вопросы по тегам:

Похожие вопросы: