Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Array in C++ to String

Tags:

c++

arrays

string

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++?

like image 864
Arjun Singh Avatar asked Dec 05 '25 15:12

Arjun Singh


1 Answers

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

like image 147
asmmo Avatar answered Dec 08 '25 10:12

asmmo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!