繁体   English   中英

我们可以用C ++制作一个映射,将值作为键的函数吗?

[英]Can we make a map in C++, with the value as a function of key?

在C ++ STL中,映射用于将键映射到值。 我想知道我们是否可以根据一些功能说来做这种映射

map <int,string> M;
和value = binary_representation(key)?

, ( )) pairs for a function and any 1 , …, you like, provided that the types match. 您可以在std::map插入任意( keyvalue )对,因此您当然可以为函数和任意 1 ,…, 插入( ))对,只要类型匹配。

直接执行此操作的方法可能是使用for循环。

for (const auto& k : keys)
  mapping[k] = f(k);

这是一个完整的示例:

#include <climits>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>

namespace /* anonymous */
{

  std::string
  my_function(const int n)
  {
    const auto size = static_cast<int>(CHAR_BIT * sizeof(int));
    std::string bits {};
    for (auto i = 0; i < size; ++i)
      {
        const auto bit = (n >> (size - i - 1)) & 1;
        bits += (bit ? '1' : '0');
      }
    return bits;
  }

}


int
main()
{
  std::map<int, std::string> reprs {};
  for (auto k = -3; k < 10; ++k)
    reprs[k] = my_function(k);
  for (const auto& kv : reprs)
    std::cout << std::setw(4) << kv.first << "  =>  " << kv.second << '\n';
}

可能的输出:

  -3  =>  11111111111111111111111111111101
  -2  =>  11111111111111111111111111111110
  -1  =>  11111111111111111111111111111111
   0  =>  00000000000000000000000000000000
   1  =>  00000000000000000000000000000001
   2  =>  00000000000000000000000000000010
   3  =>  00000000000000000000000000000011
   4  =>  00000000000000000000000000000100
   5  =>  00000000000000000000000000000101
   6  =>  00000000000000000000000000000110
   7  =>  00000000000000000000000000000111
   8  =>  00000000000000000000000000001000
   9  =>  00000000000000000000000000001001

如果您想遵循std::insert_iterator于原始循环的算法的建议,则可以将std::transformstd::insert_iterator一起使用来完成此任务。

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <string>
#include <utility>
#include <vector>

namespace /* anonymous */
{

  std::string
  my_function(int);  // as above

}

int
main()
{
  std::vector<int> keys(13);
  std::iota(keys.begin(), keys.end(), -3);
  std::map<int, std::string> reprs {};
  const auto generator = [](const int n){
    return std::make_pair(n, my_function(n));
  };
  std::transform(keys.cbegin(), keys.cend(),
                 std::inserter(reprs, reprs.begin()),
                 generator);
  for (const auto& kv : reprs)
    std::cout << std::setw(4) << kv.first << "  =>  " << kv.second << '\n';
}

但是,在这种简单情况下,我不确定使用迭代器和算法是否真的有助于代码的可读性。 这里使用keys向量有点令人讨厌。 如果您拥有Boost,可以将其替换为boost::counting_iterator

您可以这样做,但这将完全没有意义。 它只是劣等的std::set<int> ,不能正确地保证不变性,并且消耗更高的内存使用量和运行时间,而丝毫没有任何好处(除非您确实想要缓存)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM