zoukankan      html  css  js  c++  java
  • Programming Principles and Practice Using C++ Notes2

    第三章对象、类型和值

    对象:用来保存一个指定类型值的一些内存单元。

    类型:定义一组可能的值与一组运算(对于一个对象)。

    值:根据一个类型来解释的内存中的一组比特。

    #include <iostream>
    using namespace std;
    int main()
    {
            cout << "Please enter your first name and age
    "<< endl;
    
            string first_name;
            int age;
                
            cin >> first_name; 
            cin >> age;
            cout << "Hello,"<< first_name << "(age" << age <<")
    "<< endl;
    
            return 0;
    }

    这里我曾犯了个错误。我是这么写的(想当然了),结果g++了一堆错误提示。

    cin >> first_name >> endl;
    cin >> age >> endl;

     “字符串+”意味着“连接”,也就是说:

    当S1 和 S2是字符串时, S1+S2也是字符串,包含来自S1的字符后接来自S2的多个字符。

    例如:S1的值为“Hello",S2的值为”World”,那么S1+S2的值为“Hello World”

     1 #include <iostream>
     2 using namespace std;
     3 
     4 int main()
     5 {
     6         cout << "Please enter your first ande second names:
    "<< endl;
     7     
     8         string s1;
     9         string s2;
    10 
    11         cin >> s1 >> s2;     //read two strings
    12 
    13         string name = s1 +''+ s2; //concatenate strings
    14 
    15         cout << "Look,"<< name <<'
    ';
    16 
    17         return 0;
    18 
    19 }

    g++报错提示:13: error: empty character constant

    string name = s1 +' '+ s2; //单引号中间应有空格
  • 相关阅读:
    让我自闭了两个星期的题 Hello xtCpc
    kmp 回忆训练2 poj3461
    kmp 字符串匹配
    线段树之动态开点  HDU 6183 Color it.
    两个思维
    codeforces 300E Empire Strikes Back
    codeforces1392 E Omkar and Duck
    codeforces1169D
    HDU4335 欧拉函数及降幂
    HDU2588GCD(欧拉函数)
  • 原文地址:https://www.cnblogs.com/Charons/p/11171520.html
Copyright © 2011-2022 走看看