2011-08-10 8 views
1

これは小さなステージに分割します。STLの文字列ベクトルをchar *に変換するにはどうすればいいですか?

  1. は、どのように私は、ベクトル内のすべての文字列の長さの合計を計算していますか?

その空間にコピーし

  • ベクトル列をするchar * memのスペースを割り当てることを行うための簡単なエレガントな方法はありますか?

  • +0

    あなたは何を意味しますかそれは 'std :: vector 'または 'std :: vector 'ですか? –

    +0

    前者は、ベクトルに複数の文字列があると仮定しています。ベクトルかもしれませんが、そうは思われません。 – john

    +0

    または出力がchar **である必要があります。何を意味するのかを理解することは難しいです。 – amit

    答えて

    7

    私は考えることができる、最速で最もエレガントな、おそらくない方法は、次のようになります。

    std::string s = std::accumulate(v.begin(), v.end(), std::string()); 
    char * c = new char[s.size() + 1]; 
    std::strcpy(c,s.c_str()); 
    

    私は裸の動的割り当てのファン自分自身ではないよ、と個人的に使用することはありませんでしょうが、それを並べ替えるために、私はそれを残しておきます。

    あなたは要素間のスペース区切り文字を追加する必要がある場合、それは少し複雑になり、このように私はおそらく、別のソリューションを使用します。

    std::ostringstream oss; 
    std::copy(v.begin(), v.end(), std::ostream_iterator<std::string>(oss," ")); 
    size_t sz = oss.str().size(); 
    if(sz) --sz; // truncate trailing space if there is one 
    char * c = new char[sz+1]; 
    std::copy_n(oss.str().c_str(), sz, c); 
    c[sz] = 0; 
    
    +0

    私はildjarnのような何かをしただろうが、私はそれに多大な努力を払う気がしなかった。 –

    +0

    どうすれば同じことができますが、ベクトル要素の間にスペースを追加できますか?私はstd :: accumulateの演算子を変更する必要があると思います – sramij

    1

    これはエレガントな要件ではないため、私が考えることができる優雅なソリューションはありません。

    未テストコード

    vector<string> vec = ...; 
    size_t total_size = 0; 
    for (size_t i = 0; i < vec.size(); ++i) 
        total_size += vec[i].size(); 
    char* mem = new char[total_size]; 
    char* p = mem; 
    for (size_t i = 0; i < vec.size(); ++i) 
    { 
        memcpy(p, vec[i].data(), vec[i].size()); 
        p += vec[i].size(); 
    } 
    
    +0

    割り当てサイズを 'total_size + 1'に変更し、' mem [total_size] = '\ 0'; 'を追加してください。 –

    +1

    OPにヌル終了文字列が必要であるとは確信していません。彼は、たとえば、TCPソケットを介してデータを送信している可能性があります。 – john

    +0

    良い点。しかし、Cスタイルの文字列が必要な場合は、明らかに ''\ 0' 'が必要です。 –

    4

    ここでC++ 0xの実装です:

    #include <cstddef> 
    #include <algorithm> 
    #include <numeric> 
    #include <string> 
    #include <vector> 
    
    std::vector<char> concat_strings(std::vector<std::string> const& vecs) 
    { 
        std::size_t const cumulative_length = std::accumulate(
         vecs.begin(), 
         vecs.end(), 
         static_cast<std::size_t>(0), 
         [](std::size_t const acc, std::string const& str) 
         { 
          return acc + str.size(); 
         } 
        ); 
    
        std::vector<char> ret; 
        ret.reserve(cumulative_length); 
    
        std::for_each(
         vecs.begin(), 
         vecs.end(), 
         [&ret](std::string const& str) 
         { 
          ret.insert(ret.end(), str.begin(), str.end()); 
         } 
        ); 
    
        return ret; 
    } 
    

    結果std::vector<char>char*あなたの効果があります。 C++ 03の場合

    それはほとんど同じになりますが、あなたはラムダの代わりに適切なファンクタをする必要があります(または例えばBoost C++ 03のラムダ・ライブラリを使用しています。Phoenix)。

    0

    ここでは、STLの方法です:

    #include <algorithm> // std::copy 
    #include <iostream> // std::cout 
    #include <iterator> // std::ostream_iterator 
    #include <sstream> // std::ostring_stream 
    #include <string>  // std::string 
    #include <vector>  // std::Vector 
    
    using namespace std; 
    
    typedef std::vector<std::string> string_vector; 
    
    int main(int argc, char ** argv) 
    { 
        string_vector v; 
        v.push_back("string 1, "); 
        v.push_back("string 2, "); 
        v.push_back("string 3"); 
    
        std::ostringstream os; 
        std::copy(v.begin(), v.end(), std::ostream_iterator<std::string>(os, "")); 
        std::string result = os.str(); 
    
        std::cout << "result: " << result << std::endl; 
    
        return 0; 
    } 
    

    出力: "?文字列ベクトル"

    result: string 1, string 2, string 3 
    
    関連する問題