What you don't know about cout & lt; ends

Source: Internet
Author: User

Ends is a basic thing in C ++, but it may not be clearly understood by everyone. I have gone through this process, write it out and let everyone see what is wrong.

Today I used to understand ends as a tool for outputting space, or as a "" with a relatively high level of force. (This seems to be a gift from the teacher. I found the courseware was written in this way and output spaces... I believe many people think so)

Today, for some reason, cout <ends; and cout <"; seem to be not one thing, so we started to explore:

1. Explanation of cplusplus(Http://www.cplusplus.com/reference/ostream/ends? Kw = ends)

This website is the most authoritative C ++-related webpage. The definition of ends is found above:

1 _CRTIMP inline basic_ostream<char, char_traits<char> >&2  __cdecl ends(basic_ostream<char, char_traits<char> >& _O)3  {4     _O.put('\0');5      return (_O);6  }

 

From this we can hardly find that the ends actually outputs a '\ 0' character, not the space we imagined.

2. Why is there a space in the output using ends?

At the same time I saw the explanation on msdn (http://msdn.microsoft.com/zh-cn/library/a0cyxz1b.aspx), the principle of this lecture is also the same output '\ 0', but the following example:

 1 // ostream_ends.cpp 2 // compile with: /EHsc 3 #include <iostream> 4  5 int main( )  6 { 7    using namespace std; 8    cout << "a"; 9    cout << "b" << ends;10    cout << "c" << endl;11 }

 

The output is

  ab c

This is a bit confusing. Why can this ends be used as a space. So I guess it is because of system differences, so I conducted the following experiment:

1. Run in windows

1 #include<iostream>2 using namespace std;3 int main()4 {5     cout<<"ddd"<<'\0'<<"dsad"<<endl;6 }

 

Output:

Ddd dsad

2. Run the same code in Ubuntu

Output:

Ddddsad

Iii. Summary

Here I think this is the reason. In fact, C ++ processes the ends in the same way. They all Insert '\ 0' in the buffer zone and refresh it, the reason for different display conditions in different systems is that windows and linux have different processing methods for '\ 0', and a space is output in windows, in linux, there is no output.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.