C++学习的第一天

复习

输入流对象

std::cin

int a;

char c;

std::cin>>a>>c;

输出流对象

std::cout

std::cout<<"hello world"<<std::endl;

命名空间使用

避免命名污染问题(冲突)

namespace teacher
{
    char name[20];
}
​
namespace worker
{
    char name[20];
}
​
using namespace teacher;//声明下面的代码都使用teacher命名空间
using teacher::name;//下面代码使用name的时候不需要加teacher
teacher::name;//使用多个命名空间中有相同的成员。
//命名空间的作用仅仅是在名字上,对于其他比如作用域 生命周期等没有任何影响。

bool类型

基本类型(内置类型),不需要声明可以直接使用

表示真假,C++中依然可以0和非0数表示真假,但是不建议。

true false

字符串处理

C风格字符串

首地址 \0

char name[] = "hello world";
char* p = "hello farsight";

string类,不是基本类型

#include <string>  //和C语言的string.h不一样   cstring
using namespace std;
转换
string s1 = "hello";
string s2("hello");
string s3(s2);
​
s1.c_str();//调用string类的成员函数c_str(),返回值是char*
//有些函数的参数只要char*,但是此时我们只有string对象,就只能用string对象转换char*

编程1:

随机点名 定义名字数组string names[]={"rose","jack","tom"};

随机抽取一个数组中的人员

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
​
using namespace std;
​
int main()
{
    string names[] = {"jack","rose","tom"};
    srand(time(0));
    cout<<names[rand()%3]<<endl;
    return 0;
}

编程2:

  1. 定义结构体Student 结构体成员:string name

string num

编写函数实现:N个学生信息 宏定义N

  1. inputStudentInfo(Student* s)函数,需要将每个学生信息(姓名 学号)输入计算机保存 cin

3.find_stu(Student* stus, string serchName)函数,键盘输入某个学生的姓名 如果该学生存在 则输出其基本信息,

如果不存在,则输出"not found"

#include <iostream>
#include <string>
​
#define N 3
​
using namespace std;
​
struct Student
{
    string name;
    string num;
};
​
void inputStudentInfo(Student* students);
void findStu(Student* students, string searchName);
int main()
{
    Student students[3];
    inputStudentInfo(students);
    cout<<"input search name"<<endl;
    string searchName;
    cin>>searchName;
    findStu(students, searchName);
    return 0;
}
​
void inputStudentInfo(Student* students)
{
    for(int i = 0;i < N;i++)
    {
        cout<<"input name and num"<<endl;
        cin>>students[i].name>>students[i].num;
    }
}
​
void findStu(Student* students, string searchName)
{
    for(int i = 0;i < N;i++)
    {
        if(students[i].name == searchName)
        {   
            cout<<students[i].name<<" "<<students[i].num<<endl;
            return;
        }
    }
    cout<<"not found"<<endl;
}

一、引用

1.引用的作用

功能:为一个变量起别名

引用符号:& 使用数据类型加引用符号&,声明引用。

在C/C++中,出现在声明语句中的符号都不是运算符。

* 指针
[] 数组
() 函数
&  引用
​

2. 基本使用

示例1:

#include <iostream>
​
using namespace std;
​
int main()
{
//这个例子没有任何的实际的逻辑含义。
    int a = 10;//定义整型变量a
    int& b = a;//定义引用b,是变量a的引用(是变量a的别名)
    cout<<b<<endl;//10
    cout<<a<<endl;//10
    cout<<&a<<endl;//a和b的地址也是一样的,a和b是同一个变量,b仅仅a的一个别名
    cout<<&b<<endl;//
    return 0;
}

注意:1.必须初始化 2. 引用不能单独存在,也不能改变指向

3.普通引用不能用常量或者临时值来初始化

int& b = 10;//错误

示例2:

#include<iostream>
​
using namespace std;
​
int main()
{
    int a=100;
    int &ref
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值