/* adjacent_find 查詢兩個相鄰的等價元素*/
#include #include #include using namespace std;
/* adjacent_find 查詢兩個相鄰的等價元素*/
bool myfunction(int i , int j)
void adjacentfinefunction()
; vectormyvector (myints,myints+8);
vector::iterator it;
//using default comparison
it = adjacent_find(myvector.begin(), myvector.end());
if (it != myvector.end())
cout<<"the first pair of repeated elements are : "<<*it <
the first pair of repeated elements are : 5
the second pair of repeated elements are: 30
/*all_of 檢測在給定範圍中是否所有元素都滿足給定的條件*/
#include #include #include using namespace std;
/*all_of 檢測在給定範圍中是否所有元素都滿足給定的條件*/
int main(int argc, const char * argv)
; if (all_of(foo.begin(), foo.end(), (int i))) ;
if (any_of(foo.begin(), foo.end(), (int i))) ; //8 elements
int mycount = (int)count(myints, myints + 8, 10);
//counting elements in container:
vectormyvector(myints,myints + 8);
mycount = (int)count(myvector.begin(), myvector.end(), 20);
return 0;
}
輸出結果:
/*count_if 返回值滿足給定條件的元素的個數*/
/*count_if 返回值滿足給定條件的元素的個數*/
#include //std::count
#include //std::count_if
#include //std::vector
using namespace std;
bool isodd(int i)
int main(int argc, const char * argv)
int mycount = (int)count_if(myvector.begin(), myvector.end(), isodd);
cout<<"myvector contains "<
myvector contains 5odd values.
/*equal 返回兩個範圍是否相等*/
/*equal 返回兩個範圍是否相等*/
#include #include #include using namespace std;
bool mypredicate(int i, int j)
int main(int argc, const char * argv)
; vectormyvector(myints , myints + 5);
//using default comparison:
if (equal(myvector.begin(), myvector.end(), myints)) ;
int* p;
p = find(myints, myints + 4, 30);
++p;
cout<<"the element following 30 is " <<*pvector::iterator it;
it = find(myvector.begin(), myvector.end(), 30);
++it;
cout<<"the element following 30 is " << *it <
輸出結果:
the element following 30 is 40
the element following 30 is 40
/*find_end 查詢範圍a 中與範圍b等價的子範圍最後出現的位置*/
#include #include #include using namespace std;
bool myfunction(int i,int j)
int main(int argc, const char * argv)
; vectorhaystack(myints,myints + 10);
int needle1 = ;
//using default comparison:
vector::iterator it;
it = find_end(haystack.begin(), haystack.end(), needle1, needle1 + 3);
if (it != haystack.end())
cout<< "needle1 last found at position " <<(it - haystack.begin())<
needle1 last found at position 6
needle2 last found at position 4
/*find_first_of 查詢範圍
a中第乙個與範圍
b中任一元素等價的元素的位置
*/#include #include #include #include using namespace std;
bool comp_case_insensitive(char c1,char c2)
int main(int argc, const char * argv)
; vectorhaystack(mychars,mychars + 6);
vector::iterator it;
int needle = ;
//using default comparison:
it = find_first_of(haystack.begin(), haystack.end(), needle, needle + 3);
if (it != haystack.end())
cout<<"the first match is: " << *it<
/*for_each 對範圍中的每個元素呼叫指定函式*/
#include #include #include using namespace std;
void myfunction(int i)
int main(int argc, const char * argv)
; pair::iterator, int*> mypair;
//using default xomparison:
mypair = mismatch(myvector.begin(), myvector.end(), myints);
cout<<"first mismatching elements: "<<*mypair.first;
cout<< " and " <<*mypair.second#include #include #include using namespace std;
int main(int argc, const char * argv)
; if (none_of(foo.begin(), foo.end(), (int i)))
int main(int argc, const char * argv)
; vector::iterator it;
it = search(haystack.begin(), haystack.end(), needle1, needle1 + 4);
if (it != haystack.end())
cout<<"needle1 found at position " << (it - haystack.begin()) #include #include #include using namespace std;
bool mypredicate(int i,int j)
int main(int argc, const char * argv)
; vectormyvector(myints,myints + 8);
vector::iterator it;
//using default comparison:
it = search_n(myvector.begin(), myvector.end(), 2, 30);
if (it != myvector.end())
cout<<"two 30s found at position "<<(it -myvector.begin())<
C 呼叫 Fortran 寫的演算法庫的問題
今天將自己用fortran寫的演算法庫掛在c 中,執行時老是出現棧被破壞的錯誤但是 c 呼叫演算法庫的時候是沒問題的啊!後來找啊找終於發現是c 執行緒的堆疊不夠導致的!thread 建構函式 threadstart,int32 引數start 型別 system.threading.threadst...
C語言 字串內容的修改
關於 include intmain 00bbf900 00bbf900 可見這樣定義的字串是可以直接在本體上修改其內容的,因為修改前後位址並沒有變化。例2 定義乙個指標指向字串常量,然後嘗試修改字串的標點符號 include intmain 報錯 寫入訪問許可權衝突 先說結論 解釋 3 若程式試圖...
基於C語言的通用資料結構和演算法庫
本人最近在學習資料結構的課程,在過程中發現用c語言來實現各種資料結構型別的時候很難做到真正意義上的通用的資料結構,於是在網上蒐羅了一些所謂的c語言通用資料結構庫,在此也將這些資料結構庫一一羅列,方便大家查詢和使用。c語言沒有像c 那樣的stl庫,語言本身並不是一種真正意義上的高階語言,實現專案中真正...