【问题标题】:Conversion of data type using auto in C++在 C++ 中使用 auto 转换数据类型
【发布时间】:2016-10-11 12:33:53
【问题描述】:

我有 2 个向量容器,其中包含 2 种不同类型的值,数据类型为 uint32_t。我想一起打印它们。 这就是我所拥有的

vector<uint32_t> data1;
vector<uint32_t> data2;

现在我知道了一个像下面这样的单一数据的方法

for(auto const& d1: data1)
   cout<< d1 << endl;

但我想像这样一起打印两个数据,

   cout<< d1 << "\t" << d2 << endl;

如何使用自动执行此操作? (其中 d2 是从 data2 自动转换的值)

【问题讨论】:

  • data1的类型是什么?
  • vector&lt;uint32_t&gt; data1
  • 哦,所以你有vector&lt;uint32_t&gt; data1vector&lt;uint32_t&gt; data2,并且你想将data1 中的每个值与data2 中的相应值(相同索引)一起显示?
  • 很抱歉信息少。请查看已编辑的问题并提出一些建议。
  • @HarshPatel auto 不会转换类型。而且无论如何都没有理由转换。 std::cout 可以输出 unint32_t 很好。

标签: c++11


【解决方案1】:

您可以在索引上使用普通的 for 循环:

for (auto i = 0u; i != n; ++i)
    std::cout << data1[i] << "\t" << data2[i] << "\n";

编辑:例如,如果您想将uint32_t 转换为int,您可以这样做:

auto d1 = static_cast<int>(data1[i]);

但您需要确保转换安全。即该值适合目标类型。

【讨论】:

  • 我有种假设 OP 想要更花哨的东西,但是,嗯,是的。 +1。
  • @einpoklum 我仍然不确定 OP 想要什么。
  • @einpoklum 对不起,我对 stackoverflow(以及编程)还很陌生。那你能告诉我OP是什么意思吗?
  • 像“原始出版商”吗?
  • @HarshPatel “原始海报”,即你
【解决方案2】:

使用 Boost Zip Iterator,它将让您拥有一系列对而不是两个向量数据类型的范围。大致如下:

#include <boost/iterator/zip_iterator.hpp>
#include <boost/range.hpp>
#include <stdint.h>
#include <vector>
#include <iostream>

template <typename... TContainer>
auto zip(TContainer&... containers) -> boost::iterator_range<boost::zip_iterator<decltype(boost::make_tuple(std::begin(containers)...))>> {
    auto zip_begin = boost::make_zip_iterator(boost::make_tuple(std::begin(containers)...));
    auto zip_end = boost::make_zip_iterator(boost::make_tuple(std::end(containers)...));
    return boost::make_iterator_range(zip_begin, zip_end);
}

int main()
{
        std::vector<uint32_t> data1( { 11, 22, 33 } );
        std::vector<uint32_t> data2( { 44, 55, 66 } );
        for (auto t : zip(data1, data2)) {
                std::cout << boost::get<0>(t) << "\t" << boost::get<1>(t) << "\n";
        }
}

zip() 函数是由于 this question 而产生的,您可以将它放在单独的头文件中,因为它不是特定于您的情况。

【讨论】:

    【解决方案3】:

    如果可能(并且对您的用例合理):使用成对的容器

    如果您的应用程序未绑定 w.r.t。计算机资源,并且您知道您将成对使用两个容器的值(假设容器长度相同,如您的示例中所示),实际使用成对容器可能很有用,这也简化了使用整洁的基于范围的 for 循环 (>= C++11)。

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    int main()
    {
      std::vector<uint32_t> data1 = {1, 2, 3};
      std::vector<uint32_t> data2 = {4, 5, 6};
    
      // construct container of (int, int) pairs
      std::vector<std::pair<int, int>> data;
      data.reserve(data1.size());
      std::transform(data1.begin(), data1.end(), data2.begin(), std::back_inserter(data),
                     [](uint32_t first, uint32_t second) {
                       return std::make_pair(static_cast<int>(first), static_cast<int>(second));
                     });   /* as noted in accepted answer: you're responsible for
                              ensuring that the conversion here is safe */
    
      // easily use range-based for loops to traverse of the
      // pairs of your container
      for(const auto& pair: data) {
        std::cout << pair.first << " " << pair.second << "\n";
      } /* 1 4
           2 5
           3 6 */
    
      return 0;
    }
    

    【讨论】:

    • @HarshPatel 很乐意为您提供帮助。
    猜你喜欢
    • 2014-05-25
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 2022-10-05
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多