3

I know in Java, you can use Arrays.toString() to return a String representation of an Array. Is there a method that serves a similar function in C++?

3
  • what array are you talking about? char arrays are considered as a string (null terminated). Other arrays will depend on if the value type can be converted to a string. Example: integer array can be converted to a string perhaps by iterating over it and using std::to_string(), but an array of mutexes or thread objects? probably no need or way Commented Jul 11, 2020 at 4:25
  • 1
    Read a good C++ programming book then see a good C++ reference website Commented Jul 11, 2020 at 4:34
  • 2
    I know in Java, you can use Arrays.toString() to return a String representation of an Array. -- Do not use Java or any other language as a guide in learning C++. Commented Jul 11, 2020 at 5:03

2 Answers 2

1

If you mean an array of characters, you can use something like what follows

std::vector arr {'a', 'b', 'c'};//the array
std::string str{arr.cbegin(), arr.cend()};//the generated string

Working example

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

int main(){
    std::vector arr {'a', 'b', 'c'};
    std::string str{arr.cbegin(), arr.cend()};
    std::cout << str;

}

Live

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

3 Comments

std::vector requires a template argument, this probably would not compile
@WARhead, You can leave that to be deduced in C++17.
@tadman and WARhead. Please google CTAD. That is in C++ since 17.
-2

C++ is not Java. I recommend reading a good C++ programming book and to look into some C++ reference website. Be aware of various dialects of C++ (e.g. C++11 is not the same as C++17).

Remember that C++ arrays are homogeneous: all array components have the same type. In contrast to Java, C++ does not have a root type like Java Object class. But C++ has interesting standard containers. Be aware of the rule of five. And you could design your program to have your central class MyRootClass from which every other class inherits.

However, you could easily write some short code to convert an array to a string.

That being said, you could find interesting open source C++ libraries, such as POCO or Qt, to ease your work.

You could also (and easily) write your C++ metaprogram generating the C++ code for declaring arrays and emitting code to print or output them, and you might use or improve the SWIG tool for that.

Read also the documentation of your C++ compiler (e.g. GCC or Clang), your linker (e.g. GNU binutils) and of your build automation (e.g. GNU make or ninja). If your C++ compiler is GCC, compile with all warnings and debug info, so g++ -Wall -Wextra -g (and learn to use the GDB debugger). If your C++ code base is big (e.g. several hundred thousands lines of C++) consider writing your GCC plugin to generate automatically some C++ code for serialization or printing routines.

Convert Array in C++ to String

A possibility might be to use a semi-standardized textual representation like JSON or YAML (perhaps with databases like sqlite or PostGreSQL). Both have open source libraries in C++ which could be helpful.

See also the s11n serialization library.

1 Comment

An example would have been more helpful to OP, who is already expressing their awareness of the distinction between Java and C++.

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.