将 <vector><string> 转换为 <vector><int> C++, Win32
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15619696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert <vector><string> TO <vector><int> C++, Win32
提问by Carl Mark
Is there some method to convert a vector to a vector in C++, win32?
有什么方法可以将向量转换为 C++、win32 中的向量吗?
I've got this string vector with numbers:
我有这个带有数字的字符串向量:
std::vector<std::string> DataNumbers;
I need to convert this vector string into vector integer.
我需要将此向量字符串转换为向量整数。
采纳答案by user1601401
Try this:
尝试这个:
std::vector<std::string> DataNumbers;
// Fill DataNumbers
std::vector<int> intNumbers;
for (int i=0; i<= 5; i++)
{
int num = atoi(DataNumbers.at(i).c_str());
intNumbers.push_back(num);
}
回答by Joseph Mansfield
Given:
鉴于:
std::vector<std::string> DataNumbers;
// Fill DataNumbers
std::vector<int> Data;
You can use std::transform
. Use a std::back_inserter
to insert the values into the std::vector<int>
. For the unary function, use a lambda expression that uses std::stoi
to convert the strings to integers.
您可以使用std::transform
. 使用 astd::back_inserter
将值插入到std::vector<int>
. 对于一元函数,请使用std::stoi
用于将字符串转换为整数的 lambda 表达式。
std::transform(DataNumbers.begin(), DataNumbers.end(), std::back_inserter(Data),
[](const std::string& str) { return std::stoi(str); });
And here's a version without lambdas (using std::bind
instead):
这是一个没有 lambdas 的版本(std::bind
改为使用):
typedef int(*stoi_type)(const std::string&, std::size_t*, int);
std::transform(DataNumbers.begin(), DataNumbers.end(), std::back_inserter(Data),
std::bind(static_cast<stoi_type>(&std::stoi),
std::placeholders::_1, nullptr, 10));
回答by Richard J. Ross III
The C++ way of doing it is this:
C++ 的做法是这样的:
vector<std::string> input = ...;
vector<int> output;
for (auto &s : input) {
std::stringstream parser(s);
int x = 0;
parser >> x;
output.push_back(x);
}
Without knowing what you want to do when the input fails, there's not much more to say here.
不知道当输入失败时你想做什么,这里没有更多要说的。
回答by baziorek
What about:
关于什么:
#include <algorithm>
#include <boost/lexical_cast.hpp>
template<typename C1, typename C2>
void castContainer(const C1& source, C2& destination)
{
typedef typename C1::value_type source_type;
typedef typename C2::value_type destination_type;
destination.resize(source.size());
std::transform(source.begin(), source.end(), destination.begin(), boost::lexical_cast<destination_type, source_type>);
}
It can convert vector<string> into vector<int>, and also other container<T1> into container2<T2>, e.g.: list -> list.
它可以将vector<string> 转换为vector<int>,也可以将其他container<T1> 转换为container2<T2>,例如:list -> list。
Full code:
完整代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <boost/lexical_cast.hpp>
template<typename C1, typename C2>
void castContainer(const C1& source, C2& destination)
{
typedef typename C1::value_type source_type;
typedef typename C2::value_type destination_type;
destination.resize(source.size());
std::transform(source.begin(), source.end(), destination.begin(), boost::lexical_cast<destination_type, source_type>);
}
template<typename T, typename T2>
std::vector<T>& operator<<(std::vector<T>& v, T2 t)
{
v.push_back(T(t));
return v;
}
main(int argc, char *argv[])
{
std::vector<std::string> v1;
v1 << "11" << "22" << "33" << "44";
std::cout << "vector<string>: ";
std::copy(v1.begin(), v1.end(), std::ostream_iterator<std::string>(std::cout, ", "));
std::cout << std::endl;
std::vector<int> v2;
castContainer(v1, v2);
std::cout << "vector<int>: ";
std::copy(v2.begin(), v2.end(), std::ostream_iterator<int>(std::cout, ", "));
std::cout << std::endl;
}