The char str[700] in C++ string syntax

How do we convert char str[700] initializing into C++ string syntax?

1
2
3
4
5
6
7
8
int main(){
  //...
  char str[700];
   // string eqivalent...

 string str;  //...?

}

Thanks
Last edited on
Something roughly equivalent would be
 
std::string str(700, 0);
However, if you're trying to rewrite a piece of code to use C++ idioms, you should not rewrite each line in isolation; you should rewrite considering context.
It's like when you're translating, if you translate word-for-word you're going to end up with something that's possibly comprehensible but probably clunky, and at worst nonsensical. You need to understand the idea the author was trying to communicate and then express that idea in the target language.

So, for example, if your original code said
1
2
char str[700];
scanf("%s", str);
you should not replace that with
1
2
std::string str(700, 0);
std::cin >> str;
Instead, use
1
2
std::string str;
std::cin >> str;

1. Understand what the original code is supposed to accomplish.
2. Understand the classes and functions C++ provides.
3. Write C++ code that cleanly and idiomatically expresses that original intention.
Topic archived. No new replies allowed.