2013-03-25 316 views
7

是否有某种方法将矢量转换为C++,win32中的矢量?convert <vector><string> TO <vector><int> C++,Win32

我有这个字符串向量与数字:

std::vector<std::string> DataNumbers; 

我需要这个矢量字符串转换为向量整数。

+0

究竟什么是你的目标吗?数字的格式是什么?性能和可读性有多重要? – 2013-03-25 16:21:55

+0

我只需要整数。而我之后,我想从这个int矢量乘以一些数字 – 2013-03-25 16:27:06

回答

2

试试这个:

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); 
    } 
+1

我认为以前的解决方案是好的,但这对我很好,因为我的编译器没有“C++ 11功能”。 非常感谢! – 2013-03-25 16:52:31

+3

我不认为依靠硬编码的长度和使用传统的C函数是一个很好的解决方案。如果你没有C++ 11,可以使用Richard J Ross的答案并将'for(auto&s:input)'改为'for(它的数据号码为数据号码)。 end(); it ++)'以及'std :: stringstream parser(s);'to'std :: stringstream parser(* it);'。 – us2012 2013-03-25 16:59:50

+0

这段代码给了我癌症。 – Puppy 2013-09-14 09:31:11

4

的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); 
} 

不知道你想做的事,当输入失败,有没有更多的在这里说什么。

+0

谢谢。但是for循环中的“s”是什么意思? – 2013-03-25 16:25:06

+1

迭代器上的字符串矢量 – Alon 2013-03-25 16:26:04

+1

@alon不,它是字符串的副本 – jrok 2013-03-25 16:27:20

19

考虑:

std::vector<std::string> DataNumbers; 
// Fill DataNumbers 
std::vector<int> Data; 

您可以使用std::transform。使用std::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); }); 

这里还有没有lambda表达式(使用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)); 
+0

我会犹豫与lambdas疯狂。它们通常比使用for循环慢,并且会导致长期可读性。更不用说,并非所有的编译器都能完全支持它们。 – 2013-03-25 16:28:34

+0

@ RichardJ.Ross里面有一个lambda表达式,我不确定这是否符合“发疯”的标准。另外,我不认为任何编译器不支持lambda这样简单的就不会在其文档中提及关于C++ 11遵从性的任何* ... – us2012 2013-03-25 16:38:20

+1

@ us2012我可以有一个编译器支持'auto'&'decltype'并且说它具有“C++ 11功能”,不是吗?另外,我确实收到了答案。 – 2013-03-25 16:40:51

1

什么:

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

它可以转换矢量< string> into vector < int>,还有其他容器< T1>进入容器2 < T2>,例如:list - > list。

全码:

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