简体   繁体   English

如何将 integer 转换为向量,然后将该向量转换为 C++ 中的字符串

[英]How to turn an integer into vector and then turn that vector into string in C++

I want to take an integer and turn it into an array and then store it into a string in C++.我想取一个 integer 并将其转换为数组,然后将其存储为 C++ 中的字符串。 But I do not know how to turn an integer into an array and then store it into a string.但我不知道如何将 integer 转换为数组,然后将其存储为字符串。 I am still learning C++, so help me, please.我还在学习 C++,所以请帮帮我。 That's how I want it to be done:这就是我希望它完成的方式:

#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;
}

Please help me and show the code as well with explanation请帮助我并显示代码以及解释

Edit: I have some data to work with integer values with every digit which I can solve my own but for that, I need to first turn the integer into vector and return it as a string.编辑:我有一些数据可以处理 integer 值的每个数字,我可以自己解决,但为此,我需要首先将 integer 转换为向量并将其作为字符串返回。 THat's why I want to know how to turn integer into vector first and the that vector into string这就是为什么我想知道如何首先将 integer 转换为向量,然后将该向量转换为字符串

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

If you do need the steps:如果您确实需要以下步骤:

std::vector<int> res;

int num;
std::cin >> num;

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

and then接着

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

Since you insist on placing the integers in a vector first and then converting them to string later (when needed?), this can be a solution:由于您坚持首先将整数放入向量中,然后稍后将它们转换为字符串(需要时?),这可能是一个解决方案:

#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';
}

Sample I/O:示例 I/O:

1234

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

You are asking for a set of conversion functions, probably something like the code below .您要求一组转换函数,可能类似于下面的代码 The ASCII codes of the digits are in sequential order , therefore transforming a digit character to a digit means subtracting '0' from it.数字的 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");
}

Here you are:给你:

#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;
}

1)vector in c++ has vector object.push_back(); 1)c++中的向量有向量object.push_back(); // which push data(any type) to vector array 2) using an iterator function you can retrieve that data and by dereferencing(*i) you can receive that data in original form (which you had pushed) 3) use stringstream class or to_string() method to convert into string // 将数据(任何类型)推送到向量数组 2)使用迭代器 function 您可以检索该数据,并通过取消引用(* i)您可以以原始形式(您已推送)接收该数据 3)使用字符串流 class 或to_string() 方法转换成字符串

//code ` //代码`

#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;
 }
}

` `

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

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