第一節
基本框架
#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:動態陣列,從末尾能快速插入與洗掉,直接訪問任何元素
- 定義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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/250217.html
標籤:其他
下一篇:Java學習
