【问题标题】:How to turn an integer into vector and then turn that vector into string in C++如何将整数转换为向量,然后在 C++ 中将该向量转换为字符串
【发布时间】:2022-01-23 07:22:22
【问题描述】:

我想取一个整数并将其转换为数组,然后将其存储为 C++ 中的字符串。但我不知道如何将整数转换为数组,然后将其存储为字符串。我还在学习 C++,请帮助我。这就是我希望它完成的方式:

#include<iostream>
#include<vector>

using namespace std;

int main()
{
  int number = 3215;

  //store the number into vector
  vector<int> numbers;
  //vector[0]=3
  //vector[1]=2
  //vector[2]=1
  //vector[5]=5

  //and then store it into string
  string str;
  //like this
  //str=3215

  return 0;
}

请帮助我并显示代码以及解释

编辑:我有一些数据可以处理每个数字的整数值,我可以自己解决,但为此,我需要首先将整数转换为向量并将其作为字符串返回。这就是为什么我想知道如何首先将整数转换为向量,然后将该向量转换为字符串

【问题讨论】:

  • 你可以跳过vector的东西,直接把它转换成std::string或者char数组。为什么需要向量?
  • 你可以使用std::to_string

标签: c++ string vector integer c++17


【解决方案1】:

最简单的是:std::to_string(yourNum);

如果您确实需要这些步骤:

std::vector<int> res;

int num;
std::cin >> num;

while(num>0)
    {
    res.insert(res.begin(),num%10);
    num/=10;
    }

然后

std::stringstream result;
std::copy(res.begin(), res.end(), std::ostream_iterator<int>(result, ""));

【讨论】:

    【解决方案2】:

    由于您坚持首先将整数放入向量中,然后再将它们转换为字符串(当需要时?),这可能是一个解决方案:

    #include <iostream>
    #include <string>
    #include <vector>
    
    
    int main( )
    {
        std::string number;
        std::cin >> number;
    
        std::vector<int> numbers;
        numbers.reserve( number.length( ) );
    
        for ( const auto digit : number )
        {
            numbers.push_back( digit - '0' );
        }
    
        std::cout << "\nElements of vector: ";
    
        for ( const auto digit : numbers )
        {
            std::cout << digit << ' ';
        }
    
        std::cout << "\nElements of vector converted to `std::string`: ";
    
        for ( const auto num : numbers )
        {   
            std::string num_str { std::to_string( num ) };
            std::cout << num_str << ' ';
        }
    
        std::cout << '\n';
    }
    

    示例 I/O:

    1234
    
    Elements of vector: 1 2 3 4
    Elements of vector converted to `std::string`: 1 2 3 4
    
    

    【讨论】:

      【解决方案3】:

      你在这里:

      #include <iostream>
      #include <vector>
      #include <cmath>
      #include <string>
      #include <algorithm>
      #include <iterator>
      
      int main() {
          int n;
          std::cin >> n;
          std::vector<int> split(static_cast<int>(std::log10(n)) + 1);
          auto royal_10 = split.rbegin();
          auto cpy{ n };
          do {
              *royal_10++ = cpy % 10;
          } while ((cpy /= 10) != 0);
          std::string ret;
          ret.reserve(split.size());
          std::transform(split.cbegin(), split.cend(), std::back_inserter(ret),
              [](int const dig) { return dig + '0'; });
          return 0;
      }
      

      【讨论】:

        【解决方案4】:

        您需要一组转换函数,可能类似于code below。数字的 ASCII 码按顺序,因此将数字字符转换为数字意味着从中减去 '0'

        #include <iostream>
        #include <string>
        #include <vector>
        #include <algorithm>
        #include <cassert>
        
        std::vector<int> convert(const std::string& s)
        {
          std::vector<int> r;
          std::transform(s.begin(), s.end(), std::back_inserter(r), [](auto e) {return e - '0'; });
          return r;
        }
        
        std::string convert(const std::vector<int>& v)
        {
          std::string r;
          std::transform(v.begin(), v.end(), std::back_inserter(r), [](auto e) {return e + '0'; });
          return r;
        }
        
        std::vector<int> convert(const int n)
        {
          return convert(std::to_string(n));
        }
        
        int main()
        {
          auto v = convert(3215);
          auto s = convert(v);
          assert(s == "3215");
        }
        

        【讨论】:

          【解决方案5】:

          1)c++中的vector有vector object.push_back(); // 将数据(任何类型)推送到向量数组 2) 使用迭代器函数,您可以检索该数据并通过取消引用(*i) 您可以以原始形式接收该数据(您已推送) 3) 使用 stringstream 类或 to_string() 方法转换成字符串

          //代码 `

          #include<vector>
          #include<iostream>
          
          int main()
          {
           int number=1234;
           string str;
           vector <int> obj;
           obj.push_back(number);
           for(auto i= obj.begin(); i!= obj.end();i++)
           {
            str = to_string((*i));
            cout << end << str;
           }
          }
          

          `

          【讨论】:

          • 至少确保您的代码可以编译。我也很确定它不能回答 OP 的问题
          • 哥们,我是 c++ 开发人员很长时间了,我已经尝试过这个并且它有效并且问题被以错误的方式提出,我不知道他到底想做什么
          猜你喜欢
          • 2011-10-31
          • 1970-01-01
          • 2020-02-27
          • 1970-01-01
          • 2016-05-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-26
          相关资源
          最近更新 更多