2017-05-11 2 views
-4

Ich möchte String in 2D String zu teilen und Array ersten Index zu teilen, um nur die Referenz von 3 Wort und zweiten Index zu speichern, um das Wort zu speichern, um es richtig zu verstehen habe ich gegeben ein Beispiel.
Zum Beispiel string a = "Hallo, ich schreibe C++ Code";Wie man String in String 2d Array Array in C++

Ich möchte in konvertieren in

string b[100][3]; 
b[0][1]="hello"; 
b[0][2]="I"; 
b[0][3]="am"; 
b[1][1]="writing"; 
b[1][2]="c++"; 
b[1][3]="code"; 
+1

Warum brauchen Sie das? – gsamaras

+1

Was hast du probiert? Was ist deine Quelle? – NathanOliver

+1

Welche Forschung haben Sie gemacht? – Eddge

Antwort

1

Dies funktioniert und ich denke, das tun, was Sie wollen:

#include <array> 
#include <iostream> 
#include <string> 
#include <utility> 
#include <vector> 

std::vector< std::array< std::string, 3 > > split(std::string const &s) 
{ 
    std::vector< std::array< std::string, 3 > > rv; 
    std::array< std::string, 3 > a; 
    int i = 0; 
    size_t first = 0, len = s.size(); 

    while (first < len) 
    { 
     size_t last = s.find(' ', first); 
     if (last == std::string::npos) 
      last = len; 
     a[i++] = s.substr(first, last - first); 
     first = last + 1; 
     if (i == 3) 
     { 
      rv.emplace_back(std::move(a)); 
      i = 0; 
     } 
    } 
    if (i) 
     rv.emplace_back(std::move(a)); 

    return rv; 
} 

int main() 
{ 
    auto v = split("Hello I am writing c++ code"); 
    for (auto const &a : v) 
     std::cout << "'" << a[0] << "'; '" << a[1] << "'; '" << a[2] << "'\n"; 
    return 0; 
} 
1

Vektor von Vektoren eine schöne und elegante Lösung für Ihr Problem. Hier ist eine vorgeschlagene Lösung für Ihr Problem.

#include <iostream> 
#include <string> 
#include <sstream> 
#include <algorithm> 
#include <iterator> 
#include <vector> 

using namespace std; 



vector<vector<string>> strTo2DStr(const string& str, const int& r, const int& c) 
{ 
    vector<vector<string>> mat; 
    int rows(r), cols(c); 
    vector<string> words; 
    istringstream ss(str); 
    copy(istream_iterator<string>(ss),istream_iterator<string>(),back_inserter(words)); 

    int counter(0); 
    for (int i(0); i < rows; ++i){ 
     vector<string> temp; 
     for (int j(0); j < cols; ++j){ 
      if (counter < words.size()) 
       temp.push_back(words[counter++]); 
      else 
       temp.push_back(""); 
     } 
     mat.push_back(temp); 
    } 

    return mat; 
} 

int main() 
{ 
    string str("Hello I am writing c++ code"); 
    int rows(3), cols(3); 
    vector< vector<string> > mat = strTo2DStr(str,rows,cols); 

    cout << str << endl << endl; 
    for (int i(0); i < rows; ++i) 
     for (int j(0); j < cols; ++j) 
      cout << "mat[" << i << "]["<< j << "]= " << mat[i][j] << " " << endl; 


    return 0; 
} 

Die Lösung ist flexibel in dem Sinne, dass Sie die Anzahl der Zeilen und Spalten auswählen können. Das Ergebnis ist

Hello I am writing c++ code 
mat[0][0]= Hello 
mat[0][1]= I 
mat[0][2]= am 
mat[1][0]= writing 
mat[1][1]= c++ 
mat[1][2]= code 
mat[2][0]= 
mat[2][1]= 
mat[2][2]=