Is there a way to copy string from const char* to string in C++? I'm trying to do something like this:
#include <iostream>
#include <string.h>
using namespace std;
int main(){
string s1;
const char* pStr = "Sample Text";
for(int i = 0; i < strlen(pStr); i++)
s1[i] = pStr[i];
cout<<s1<<endl;
return 0;
}
In my case s1 turns out to be empty.
Take a look in cppreference, the constructors of
std::basic_string(the template class behindstd::string), alternative (5):