输入:

二维动态数组:

 

int** a2 = new int*[rows];

 

for(int i=0;i<rows;i++)

a2[i] = new int[columns];

 

 

若只用cin,cout,可以添加

std::ios::sync_with_stdio(false);

加快读取速度

 

输入两个字符串,中间以空格分隔:

#include<stdio.h>

scanf("%s%s",rule[i],name[i]);

其中char rule[105][105];

 

取长度:

#include<string.h>

strlen(rule);

 

读取一行字符串,其中以空格分隔

#include<sstream>

string line;

getline(cin, line);

stringstream ss(line);

while (ss >> x)

{

cout << "ss字符串流中第 " << i++<< " 个数为:";

cout << x << endl;

}

 

若保存在char数组里:

getchar();         //#include<stdio.h>

//此函数不用循环!!!!

string line;

getline(cin, line);

stringstream ss(line);

string temp;

while (ss >> temp)           //空格会分割!!!

{

//从temp[0]开始取值

//记得把temp清空

}

  1. 把字符串转为整形

#include <stdlib.h>

#include <cstring>

string temp;

int a = atoi(temp.c_str()+1);//取temp字符串从第二个字符往后数的字符串

//如,输入R10,则a = 10。

 

输出:

  1. 保留两位小数

#include<iomanip>

cout<<setiosflags(ios::fixed)<<setprecision(2)<<(float)x

 

  1. 保留有效位数

cout<<setprecision(5)<<

 

各类创建:

char型字符串:

const char *a;

a = abcde;//相当于字符串

 

队列初始化:

queue<int> task;

for(int j=0;j<n;j++)

{

while(!task.empty()) task.pop();

}

队列使用front前,一定要先判断是否为空!!!

初始化:

数组赋初值:

int a[5] = {0};//全部为0

String s;      //对象类,可不赋默认初值(自动调用构造函数)

 

 

常用方法:

String:

append() – 在字符串的末尾添加字符

find() – 在字符串中查找字符串

insert() – 插入字符

length() – 返回字符串的长度

replace() – 替换字符串

substr() – 返回某个子字符串

 

 

各类型互相转化

String 转Int:

int n = atoi(str.c_str())

...转string:

to_String();

 

Vector:

 

迭代查找

for(vector<int>::iterator i=G[cur].begin();i!=G[cur].end();i++){ if(vis[*i]==0){ dfs(*i,sta); } }

 

取队首元素:

Mes mes = pro[n].mess.front();

 

取栈顶元素:

Int x = num.top();

 

按结构体的元素排序:

bool cmp(const node &x,const node &y)

{

Return x.v<y.v;

}

sort(ad,ad+n,cmp);

 

 

其他:

int 32位,能表示109左右。

Long long 64位。

 

Bool取反:

Index = !Index;

 

 

判断某一天是星期几:

int year,month,day;

    cin>>year>>month>>day;

    if(month<3)

    {

        year-=1;

        month+=12;

    }

    int c=year/100;

    int y=year%100;

    int w=((c/4)-2*c+(y+y/4)+(13*(month+1)/5)+day-1)%7;

    switch(w)

    {

        case 0:cout<<"Sunday";break;

        case 1:cout<<"Monday";break;

        case 2:cout<<"Tuesday";break;

        case 3:cout<<"Wednesday";break;

        case 4:cout<<"Thursday";break;

        case 5:cout<<"Friday";break;

        case 6:cout<<"Saturday";break;

    }

 

大小写不敏感的字符串相等判断

bool sCmpIgnoCase(const string &s1, const string &s2)

{

int sl = s1.length();

int tl = s2.length();

string s3 = s1;

string s4 = s2;

for (int i = 0; i < sl; i++)

{ //C++中字符串 转换 大小写的方法 toupper(s1[i]);

if (s1[i] >= 65 && s1[i] <= 90) s3[i] = tolower(s3[i]);

}

for (int j = 0; j < tl; j++)

{

if (s4[j] >= 65 && s4[j] <= 90) s4[j] = tolower(s4[j]);

}

int ans = s3.find(s4, 0);

if (ans != s3.npos)

return true;

else return false;

}

csp c++技巧总结

csp c++技巧总结

 

相关文章:

  • 2022-12-23
  • 2021-10-22
  • 2022-02-03
  • 2021-12-22
  • 2021-07-12
  • 2021-09-19
  • 2021-08-09
  • 2021-12-05
猜你喜欢
  • 2022-02-24
  • 2022-12-23
  • 2021-08-21
  • 2021-11-17
  • 2021-07-04
  • 2022-12-23
相关资源
相似解决方案