2021 1.16

第一节
基本框架
#include
using namespace std;
int main()
{
int n,m ;
cin>>n>>m;
swap(n,m);
cout <<n<<’’<<m;
return 0;
}
C++上可以用C
只需要将 .h变成c

#include<math.h>#include
Endl 换行符
/n
String类

#include
String 一种字符串类型
string类是由头文件< string >支持的(注意,头文件< string.h >和< cstring >
支持对C风格字符串进行操纵的C库字符串函数,但不支持string类)。

#include
#include
using namespace std;
int main()
{
string str; //构造名为str的空字符串
cout<<str<<endl;//检测为空

string str1("i like");//为字符串str1赋值
cout<<str1<<endl;

str1=str1+" ACM"; //在str1后面添加字符串
cout<<str1<<endl;

string str2(str1);//将字符串str2初始化为str1,相当于复制
cout<<str2<<endl;

string str3(10,'A'); //将str3初始化为由10个A组成的字符串
cout<<str3<<endl; 

string str4("Kobe Byrant",4); //访问前四个元素
string str5("Kobe Byrant",5,6);//访问从第五个开始往后的6个元素
cout<<str4<<" "<<str5<<endl;

}
string s(“To be or not to be”);
cout<<s.length();
string s(“To be or not to be”);
cout<<s.size()😭 长度 )
vector
vector:动态数组,从末尾能快速插入与删除,直接访问任何元素

  1. 定义int型数组(也可使char、double等类型)
    #include
    #include
    using namespace std;
    int main()
    {
    vector< int >a;//默认初始化,a为空
    }

定义结构型数组
#include
#include
using namespace std;
struct point{
int x,y;
};
int main()
{
vector< point >a;//a用来存坐标
}
定义 string 型元素
#include
#include
using namespace std;
int main()
{

vector< string >a(10,"null");//10个值为null的元素;
vector< string >vec(10,"hello");//10个值为hello的元素

}
定义二维数组
vectora[5];
}#include<bits/stdc++.h>
#include
using namespace std ;
int main()
{
vectora[10];
for( int i=0;i<5;i++)
{
for(int j=0;j<4;j++)
{
a[i].push_back(6);
}
}
for(int i=0;i<5;i++)
{
for(int j=0;j < 4; j++)
{
cout <<a[i][j]<<" ";

	  }
	  	cout<<endl;
   }		
}

判断是否为空
bool isEmpty
isEmpty=a.empty()
cout<<isEmpty; // 若为空返回1否则返回0;
5. 中间插入:在第i个元素前面插入
6. a.insert(begain()+I,k );
7. 尾部插入 a.push_back();
8. 排序,基于快排(非常常用)
sort (a,begain,a end);

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值