Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a new line inside a string in C++?

Tags:

string

c++11

I was thinking if it is possible to make a string in C++ which contains data in it like, I don't want to make a string of strings or an array of strings.

Suppose I have a string mv:

mv =  
"hello         
 new                   
 world "

"hello", "new" and "world" are in different lines. Now if we print mv, then "hello", "new" and "world" should come on different lines.

I was also thinking with respect to competitive programming. If I concatenate all the answers of queries in a single string and then output the answer, or cout all the queries one by one, will there be a time difference in both the outputs?

like image 928
Mrinal Verma Avatar asked Dec 05 '22 14:12

Mrinal Verma


2 Answers

i want my string variable to store the information in this format therefore like my string variable mv should have some strings in first line and some strings on second line and this whole should work like a single string

What is a string (std::string) in C++?

It is a container for a dynamically resizable array of characters, equipped with methods for manipulating that array.

An array of characters is:

characters: |c0|c1|c2|c3|...|cN|

That's the nature of an std::string. There's nothing you can do about that.

What is a line (of text)?

There is no formal definition of a line in C++, or any other programming language. A line is a visual concept that belongs to reading and writing text arranged in 2-dimensional space. One line is vertically above or below another.

Computers don't arrange data in 2-dimensional space. They arrange it all in linear, 1-dimensional, storage. No data is vertically above or below any other data.

But of course programming languages can represent lines of text and they all do it by the same convention. Conventionally, a line is an array of characters that ends with a new-line sequence.

A new-line sequence is itself an array of one or two characters, depending on your operating system's convention. Windows uses the 2-character sequence carriage-return,line-feed. Unix-like operating systems use the 1-character sequence line-feed. You can study the subject in Wikipedia: Newline. But for portability, in C++ source code the newline sequence - whatever it actually is - is represented by the escape sequence \n, which you can use in source code as if it were a character.

So the array of characters:

    |h|e|l|l|o|\n|

represents the line of text:

hello

in C++. And the array of characters:

    |h|e|l|l|o|\n|n|e|w|\n|w|o|r|l|d|\n|

represents the three lines of text:

hello
new
world

And if you want to store that array in a single std::string, C++ lets you do it like this:

std::string s0{'h','e','l','l','o','\n','n','e','w','\n','w','o','r','l','d','\n'};

or more conveniently like this:

std::string s1{"hello\nnew\nworld\n"};

or even - if you have a phobia about using the \n escape sequence - like this:

std::string s2
{R"(hello
new
world
)"};

All of these ways of storing the three lines in a string create exactly the same character array in that string, namely:

    |h|e|l|l|o|\n|n|e|w|\n|w|o|r|l|d|\n|

They all create exactly the same std::string.

And if you print any of those strings you will see the same thing, e.g. this program:

#include <string>
#include <iostream>

int main() {
    std::string s0{'h','e','l','l','o','\n','n','e','w','\n','w','o','r','l','d','\n'};
    std::string s1{"hello\nnew\nworld\n"};
    std::string s2 // Yes...
{R"(hello
new
world
)"}; // ...it is meant to be formatted like this
    std::cout << "--------------" << std::endl;
    std::cout << s0;
    std::cout << "--------------" << std::endl;
    std::cout << s1;
    std::cout << "--------------" << std::endl;
    std::cout << s2;
    std::cout << "--------------" << std::endl;
    return 0;
}

outputs:

--------------
hello
new
world
--------------
hello
new
world
--------------
hello
new
world
--------------

Live demo

like image 174
Mike Kinghan Avatar answered Dec 08 '22 02:12

Mike Kinghan


If I haven't understood your question wrong. Here is your answer, add an escape character at the end of each line.

string mv = "hello\n\
new\n\
world\n";

\n -> new line
\ -> escape character

Here is the working example: Example

string mv = "hello\n\ new\n\ world\n";

like image 28
Karan Arora Avatar answered Dec 08 '22 02:12

Karan Arora