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.

8
  • 1
    std::string from #include <string> is the class type for string in c++ Commented Nov 21, 2017 at 14:11
  • 5
    Better choose a good book. Will tell you everything you need to know about std::string. Commented Nov 21, 2017 at 14:11
  • 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. Commented Nov 21, 2017 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 Commented Nov 21, 2017 at 14:13
  • 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++. Commented Nov 21, 2017 at 14:20

4 Answers 4

13

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.

Sign up to request clarification or add additional context in comments.

8 Comments

can this array be handled just like a normal array?
@JavaRookie: Indeed it can. C++11 is very powerful.
@JavaRookie You can use the subscript operator in the usual way. E.g. foo[0] to get the first string.
is #include<string.h> required for using this?
@JavaRookie Turbo C++ is an ancient compiler which came before C++ had even reached its first official version. Use a compiler from this millenium, such as GCC or Clang.
|
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:

2 Comments

Nice links at the bottom, making this answer useful. Feel free to pinch the initialisation from my answer.
@Bathsheba I'd like a merge button for those kind of cases.
6

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.