String-Array



  • Hallo,

    ich möchte gerne ein String-Array anlegen.

    In Java würde das so aussehen:

    String strArray [3] = new String ();
    strArray[0] = "abc";
    strArray[1] = "def";
    strArray[2] = "ghi";
    

    oder

    String strArray[] = {"abc", "def", "ghi"};
    

    Wie kann ich das in C++ machen?



  • Hierzu ein kleines Beispiel:

    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    template <class T>
    void print_(T a)
    {
      cout << a << ' ';
    }
    
    int main()
    {
      string v[3] = { "Kein", "langweiliger", "Standardspruch" };
    
      string v2[3];
      v2[0] = "Kein";
      v2[1] = "langweiliger";
      v2[2] = "Standardspruch";
    
      vector<string> v3(3);
      v3[0] = "Kein";
      v3[1] = "langweiliger";
      v3[2] = "Standardspruch";
    
      for_each(&v[0], &v[3], print_<string>);
      cout << endl;
    
      for_each(&v2[0], &v2[3], print_<string>);
      cout << endl;
    
      for_each(v3.begin(), v3.end(), print_<string>);
      cout << endl;
    }
    


  • Hallo

    #include <string>
    ...
    std::string strArray[] = {"abc", "def", "ghi"};
    

    bis bald
    akari


Anmelden zum Antworten