【刷题笔记1】

笔记1

cpp 复制代码
 string s;
    while(cin>>s);
    cout<<s.length()<<endl;

输入为hello nowcoder时,输出为8 (nowcoder的长度)

2.字符串的输入(有空格)

cpp 复制代码
 string a;
    getline(cin, a);
    cout<<a<<endl;

输入为ABCabc a

输出为ABCabc a

否则,就用一般性的cin>>a;cout<<a;

3.排序函数sort(first,last)

cpp 复制代码
//#include <algorithm>(要添加头文件)
 int a[10] = {9, 6, 3, 8, 5, 2, 7, 4, 1, 0};
    sort(a, a + 10);  // 10为元素个数

没有第三个参数,默认升序。

如果降序排序:

cpp 复制代码
//#include <algorithm>(要添加头文件)
sort(a, a + 10, greater<int>());  // 10为元素个数

4.合并键值对(map的使用)

cpp 复制代码
//#include <map>(要添加头文件)
  map <int,int> m;  //定义
   cin>>n;
   for (int i=0;i<n;i++)
   {
    cin>>x>>y;  //赋值
    m[x]+=y;     //合并
   }
   for(map<int,int>::iterator it=m.begin();it!=m.end();it++)
   {
    cout<<it->first<<" "<<it->second<<endl;
   }

5.字符与字符串的熟悉巩固

cpp 复制代码
string a1;
a1={'a','b','c'};  //等效于string a1={"abc"};
char a2=a1[1];//索引的形式
cout<<a1<<endl;//输出结果为abc



char b=66;
cout<<b<<endl;//输出结果为B

char b='66';
cout<<b<<endl;//输出结果为66

char b[]="student"  //这个叫字数组

6.stake(堆栈)的使用

定义: stack<数据类型> 容器名

常用函数:

empty() //判断堆栈是否为空

pop() //弹出堆栈顶部的元素

push() //向堆栈顶部添加元素

size() //返回堆栈中元素的个数

top() //返回堆栈顶部的元素

cpp 复制代码
#include<stack>   //头文件

int main() {
 string a;
 stack<string>s;   //定义堆栈的形式
 while(cin>>a)     //控制输入
 {    s.push(a);   }    //压栈

 while(!s.empty())    //判断非空
 {    cout<<s.top()<<" ";    //顶部元素
      s.pop();                     //循环弹出堆栈顶部元素
 }
}
  1. 容器(vector)的使用
    定义: stack<数据类型> 容器名
cpp 复制代码
vector<string>a1(n);
	int n;
	cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a1[i];  //给容器赋值
    }
    sort(a1.begin(),a1.end());  //排序。begin/end 都是容器的常规操作

    for(int i=0;i<n;i++)
    cout<<a1[i]<<endl;
相关推荐
肆忆_15 小时前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星19 小时前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛3 天前
delete又未完全delete
c++
端平入洛4 天前
auto有时不auto
c++
西岸行者4 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
哇哈哈20214 天前
信号量和信号
linux·c++
多恩Stone4 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
starlaky4 天前
Django入门笔记
笔记·django
勇气要爆发4 天前
吴恩达《LangChain LLM 应用开发精读笔记》1-Introduction_介绍
笔记·langchain·吴恩达
蜡笔小马4 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost