1

As far as I know,in C++ string is itself an array of char.

So my question is:
Is it possible to have a String array in C++?

If yes please let me know the way to declare/handle it.

Java Rookie
  • 432
  • 3
  • 12
  • 1
    `std::string` from `#include ` is the class type for string in c++ – user2807083 Nov 21 '17 at 14:11
  • 4
    Better [choose a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Will tell you everything you need to know about `std::string`. – StoryTeller - Unslander Monica Nov 21 '17 at 14:11
  • What's wrong with `std::string`? – Cory Kramer Nov 21 '17 at 14:12
  • The proper type to use is `std::string` or `std::u16string`, if you want UTF16. And yes, you can create containers of strings. – Panagiotis Kanavos Nov 21 '17 at 14:12
  • 2
    _"String is itself an array of char"_ - no. C uses char arrays for strings, but doesn't have a type named String. C++ _can_ use those arrays, but `std::string` is mostly preferred. – Useless Nov 21 '17 at 14:13
  • 1
    Be careful not to mix up C with C++. You can compile C code with C++ but that doesn't make it a good idea – Panagiotis Kanavos Nov 21 '17 at 14:13
  • in c++ is c++ an array of characters? – Java Rookie Nov 21 '17 at 14:16
  • 1
    In Java (up to Java 8), String is also an array of characters. It's just that you can't get at it, because it's declared a private field. Similarly with C++ strings. In C, they're a bit more explicit but you can still have arrays of char arrays. It's just that in C++ it's not recommended to use C strings. Stick to C++. – Klitos Kyriacou Nov 21 '17 at 14:20

4 Answers4

12

Of course it is:

// Requires <string> and <vector> includes
std::vector<std::string> foo = {"this", "is", "a", "string", "array"};

or

// Requires <string> and <array> includes
std::array<std::string, 3> foo = {"if", "you", "must"};

As a rule of thumb, always use std::vector unless you can think of a very good reason not to.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
7

In modern C++, we try not to see strings as array of characters anymore. Some high-level tools from the Standard Library provide the level of indirection we need.

Array of five strings:

std::array<std::string, 5> = {"init.", "generously", "provided", "by", "Bathsheba" };

Dynamic array of strings:

std::vector<std::string> = { "as", "much", "strings", "as", "one", "wants" };

Please see their related documentation:

YSC
  • 38,212
  • 9
  • 96
  • 149
5

String is a type. As with any other type it is possible to define an array of strings in C++:

std::string myarray[] = {"Hello", "World", "Lorem"};

or:

std::vector<std::string> myarray = {"Hello", "World", "Lorem"};

or:

std::array<std::string, 3> myarray = {"Hello", "World", "Lorem"};

Be sure to include the <string> and other appropriate headers. Here is more info on std::string class template.

Ron
  • 14,674
  • 4
  • 34
  • 47
4

Here is what you want literally:

#include <string>
// ...
std::string str_array[] = {"some", "strings", "in", "the", "array"};

But actually you'd better use std::array or std::vector as it shown in others answers.

user2807083
  • 2,962
  • 4
  • 29
  • 37