c++引号里的输入输出有关问题
c++引号里的输入输出问题
今天照着书上的代码敲了下,这一句没看太懂
cout << ival1 << "+("<<ival2<<")"<< "=" << ival1 + ival2 << endl;
书上是这样写的,不过我对引号的作用不太理解,刚开始自己是这么写的
cout << ival1 << "+(<<ival2<<)" << "=" << ival1 + ival2 << endl;
为什么 "+("<<ival2<<")"就能显示iva2定义的值,而像"+(<<ival2<<)" 就只能显示+<<ival2<<
源代码如下(顺带问下,书上是有#include<string>的,不过我不知道为什么要加上这个,自己敲的时候也没加这个,不过还是出了结果,请问这个源代码里string可加可不加吗? 求解答,many thanks
#include<iostream>
using namespace std;
void main()
{
int ival1, ival2;
cin >> ival1 >> ival2;
if (ival2 >= 0)
cout << ival1 << "+" << ival2 << "=" << ival1 + ival2 << endl;
else
cout << ival1 << "+("<<ival2<<")" << "=" << ival1 + ival2 << endl;
if (ival1%ival2 == 0)
cout << ival1 << "%" << ival2 << "==0" << endl;
else
cout << ival1 << "%" << ival2 << "!=0" << endl;
}
------解决思路----------------------
cout << ival1 << "+("<<ival2<<")"<< "=" << ival1 + ival2 << endl;
cout << ival1 << "+(<<ival2<<)" << "=" << ival1 + ival2 << endl;
双引号是把引起来的常量输出,要输出的变量不能放在引号里面,像第二句就把+(<<ival2<<)当成一个常量输出
#include<iostream>
using namespace std;
void main()
{
int ival1, ival2;
cin >> ival1 >> ival2;
if (ival2 >= 0)
cout << ival1 << "+" << ival2 << "=" << ival1 + ival2 << endl;
else
cout << ival1 << "+("<<ival2<<")" << "=" << ival1 + ival2 << endl;
if (ival1%ival2 == 0)
cout << ival1 << "%" << ival2 << "==0" << endl;
else
cout << ival1 << "%" << ival2 << "!=0" << endl;
}
应该是不用include string的头文件的,如果要用处理字符串的函数时需要
------解决思路----------------------
引号内的东西只作为字符串处理
另外如果想输出引号,需要用\"来表示
------解决思路----------------------
第一个问题,在c++里,”abc“ 是一种文字常量(literal constant),就好比你在程序中些数字一样,文字常量也是有自己的类型的,当你写数字11这个文字常量时,它的类型是int,当你写11.0时,它的类型是double,当你写”abc“时,他的类型是const char[4],也就是说,它的类型是常量字符数组,而数组的长度要加上表示字符串结尾的'\0'这个字符。这算是会打了你第一个问题。
第二个问题,不用包含string。因为你没用string相关的操作,标准IO库重载了const char ×的 <<运算符,但是你还要了解一个规则才能明白,数组到指针的转换,在c++里自动发生的,而且是精确匹配,以上面的例子const char[4]到const char ×是精确匹配,
所以cout<<“abc” 会调用ostream & operator<<(ostream &,const char *)这个函数
你可能现在还不明白我在说什么,但是随着你后面深入的学习,你会慢慢了解c++的习性,这里涉及的知识点是
1、文字常量的缺省类型
2、<< 操作符的overload
3、c++定义的精确转换,其中 array to pointer,function to function pointer是语言规定的。
等你深入学习了这些只是点,再来回味,一切就好理解了。
c++学习的切入点很难,因为可能在讲解过程中,不涉及为讲解的知识,所以你需要记录没有讲解的知识点,回头讲到了再来回味。
------解决思路----------------------
今天照着书上的代码敲了下,这一句没看太懂
cout << ival1 << "+("<<ival2<<")"<< "=" << ival1 + ival2 << endl;
书上是这样写的,不过我对引号的作用不太理解,刚开始自己是这么写的
cout << ival1 << "+(<<ival2<<)" << "=" << ival1 + ival2 << endl;
为什么 "+("<<ival2<<")"就能显示iva2定义的值,而像"+(<<ival2<<)" 就只能显示+<<ival2<<
源代码如下(顺带问下,书上是有#include<string>的,不过我不知道为什么要加上这个,自己敲的时候也没加这个,不过还是出了结果,请问这个源代码里string可加可不加吗? 求解答,many thanks
#include<iostream>
using namespace std;
void main()
{
int ival1, ival2;
cin >> ival1 >> ival2;
if (ival2 >= 0)
cout << ival1 << "+" << ival2 << "=" << ival1 + ival2 << endl;
else
cout << ival1 << "+("<<ival2<<")" << "=" << ival1 + ival2 << endl;
if (ival1%ival2 == 0)
cout << ival1 << "%" << ival2 << "==0" << endl;
else
cout << ival1 << "%" << ival2 << "!=0" << endl;
}
------解决思路----------------------
cout << ival1 << "+("<<ival2<<")"<< "=" << ival1 + ival2 << endl;
cout << ival1 << "+(<<ival2<<)" << "=" << ival1 + ival2 << endl;
双引号是把引起来的常量输出,要输出的变量不能放在引号里面,像第二句就把+(<<ival2<<)当成一个常量输出
#include<iostream>
using namespace std;
void main()
{
int ival1, ival2;
cin >> ival1 >> ival2;
if (ival2 >= 0)
cout << ival1 << "+" << ival2 << "=" << ival1 + ival2 << endl;
else
cout << ival1 << "+("<<ival2<<")" << "=" << ival1 + ival2 << endl;
if (ival1%ival2 == 0)
cout << ival1 << "%" << ival2 << "==0" << endl;
else
cout << ival1 << "%" << ival2 << "!=0" << endl;
}
应该是不用include string的头文件的,如果要用处理字符串的函数时需要
------解决思路----------------------
引号内的东西只作为字符串处理
另外如果想输出引号,需要用\"来表示
------解决思路----------------------
第一个问题,在c++里,”abc“ 是一种文字常量(literal constant),就好比你在程序中些数字一样,文字常量也是有自己的类型的,当你写数字11这个文字常量时,它的类型是int,当你写11.0时,它的类型是double,当你写”abc“时,他的类型是const char[4],也就是说,它的类型是常量字符数组,而数组的长度要加上表示字符串结尾的'\0'这个字符。这算是会打了你第一个问题。
第二个问题,不用包含string。因为你没用string相关的操作,标准IO库重载了const char ×的 <<运算符,但是你还要了解一个规则才能明白,数组到指针的转换,在c++里自动发生的,而且是精确匹配,以上面的例子const char[4]到const char ×是精确匹配,
所以cout<<“abc” 会调用ostream & operator<<(ostream &,const char *)这个函数
你可能现在还不明白我在说什么,但是随着你后面深入的学习,你会慢慢了解c++的习性,这里涉及的知识点是
1、文字常量的缺省类型
2、<< 操作符的overload
3、c++定义的精确转换,其中 array to pointer,function to function pointer是语言规定的。
等你深入学习了这些只是点,再来回味,一切就好理解了。
c++学习的切入点很难,因为可能在讲解过程中,不涉及为讲解的知识,所以你需要记录没有讲解的知识点,回头讲到了再来回味。
------解决思路----------------------
cout << ival1 << "+("<<ival2<<")"<< "=" << ival1 + ival2 << endl;
// 1 2 1 2 1 2
cout << ival1 << "+(<<ival2<<)" << "=" << ival1 + ival2 << endl;
// 1 2 1 2
//每对12之间照原样输出