学习中遇到的c++问题,持续更新
原文請(qǐng)?jiān)L問(wèn)我的博客:http://xiaoshig.sinaapp.com/
向上取整
使用ceil函數(shù)。ceil(x)返回的是大于x的最小整數(shù)。如: ceil(2.5) = 3 ceil(-2.5) = -2 sort排序頭文件#include <algorithm>?
數(shù)組初始化總結(jié)
整型數(shù)組初始化://僅僅能賦值0,賦其它值用memset(a,要賦的值,sizeof(a));
char a[3][4]={0};
字符數(shù)組初始化:
int b[3][4]={0};
布爾型數(shù)組初始化:
bool c[5]={0};
結(jié)構(gòu)體初始化:
struct instruction{ //定義結(jié)構(gòu)體,存儲(chǔ)指令
int head; //識(shí)別指令
int d;
int n; //指令附加內(nèi)容
}pro[1000]={0}; //存儲(chǔ)程序體,相當(dāng)于RAM
初始化之后都會(huì)變成0。
(char數(shù)組變?yōu)閈000,int數(shù)組變?yōu)?,bool數(shù)組變?yōu)閒alse,這個(gè)樣例里的結(jié)構(gòu)體全部元素的每個(gè)成員值都為0)
sort()函數(shù)的使用
?
sort()和qsort()一樣,都是對(duì)數(shù)組的指定部分排序。qsort()僅僅使用了快排。sort()使用了混合排序。相比之下,sort()更快一些。
sort()函數(shù)經(jīng)常使用的有兩種形式,兩個(gè)參數(shù)的形式,和三個(gè)參數(shù)的形式。
1、兩參數(shù):sort(數(shù)組名。數(shù)組末地址); //比如:sort(a+1,a+n+1);就是對(duì)a[1]...a[n+1]進(jìn)行排序。默認(rèn)是升序排序。假設(shè)想改變排序順序,須要另寫一個(gè)比較函數(shù)
2、三參數(shù):sort(數(shù)組名,數(shù)組末地址。比較函數(shù));
比如:
bool cmp(const int a,const int b) {return a<b; } sort(a+1,a+10+1,cmp);就是對(duì)a[1]...a[n+1]進(jìn)行從大到小排序。
string類庫(kù)
演示樣例鏈接
substr 方法?
?返回一個(gè)從指定位置開始的指定長(zhǎng)度的子字符串。 stringvar.substr(start [開始的數(shù), length ]) ?參數(shù) ?stringvar ?必選項(xiàng)。
要提取子字符串的字符串文字或 String 對(duì)象。 start ?必選項(xiàng)。
所需的子字符串的起始位置。
字符串中的第一個(gè)字符的索引為 0。 length ?可選項(xiàng)。
在返回的子字符串中應(yīng)包含的字符個(gè)數(shù)。 說(shuō)明 ?假設(shè) length 為 0 或負(fù)數(shù)。將返回一個(gè)空字符串。假設(shè)沒(méi)有指定該參數(shù),則子字符串將延續(xù)到 stringvar 的最后。?
演示樣例 ?以下的演示樣例演示了substr 方法的使用方法。
<pre name="code" class="cpp">function SubstrDemo(){ var s, ss; //聲明變量。 var s = "The rain in Spain falls mainly in the plain."; ss = s.substr(12, 5); //獲取子字符串。
return(ss); //返回 "Spain"。
查找字符串a(chǎn)是否包括子串b。不是用strA.find(strB) > 0而是strA.find(strB) != string:npos
algorithm 簡(jiǎn)單使用方法
#include "stdafx.h" #include <iostream> #include <vector> #include <algorithm> using namespace std;int student_Score[] = { 50,80,93,23,66};void pritit(int nScore) {cout<<nScore<<" "; } bool unPass(int nScore) {return nScore < 60; } bool Pass(int nScore) {return nScore >= 60; }int main(int argc, char* argv[]) {vector<int> v_score(student_Score,student_Score+sizeof(student_Score)/sizeof(int));vector<int>::iterator index;//排序sort(v_score.begin(),v_score.end()); //顯示for_each(v_score.begin(),v_score.end(),pritit); cout<<endl;//顯示最小index = min_element(v_score.begin(),v_score.end());cout<<"最小分?jǐn)?shù) "<<*index<<endl;//顯示最大index = max_element(v_score.begin(),v_score.end());cout<<"最大分?jǐn)?shù) "<<*index<<endl;//顯示不低于60分的數(shù)量cout<<"低于60的數(shù)量 " <<count_if(v_score.begin(),v_score.end(),unPass)<<endl;//高于60的數(shù)量cout<<"高于60的數(shù)量 "<<count_if(v_score.begin(),v_score.end(),Pass)<<endl;//平均數(shù)int sum = 0;for (index = v_score.begin(); index != v_score.end(); index++){sum += *index;}cout<<"平均數(shù) "<<sum / v_score.size() <<endl;return 0; }
轉(zhuǎn)載于:https://www.cnblogs.com/yfceshi/p/6735013.html
總結(jié)
以上是生活随笔為你收集整理的学习中遇到的c++问题,持续更新的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Python os 属性(便于跨平台开发
- 下一篇: springmvc ajax 页面无法重