Reputation: 149
I want to take an integer and turn it into an array and then store it into a string in C++. But I do not know how to turn an integer into an array and then store it into a string. I am still learning C++, so help me, please. 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. THat's why I want to know how to turn integer into vector first and the that vector into string
Upvotes: 1
Views: 630
Reputation:
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;
}
Upvotes: 1
Reputation: 4808
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.
#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");
}
Upvotes: 0
Reputation: 3672
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:
1234
Elements of vector: 1 2 3 4
Elements of vector converted to `std::string`: 1 2 3 4
Upvotes: 1
Reputation: 1
1)vector in c++ has vector 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
//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;
}
}
`
Upvotes: -2
Reputation: 1137
The easiest is : 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, ""));
Upvotes: 1