我發現從根據第一個字母陣列中的所有字符串上:find_if一個字符串數組
#include<iostream>
#include<algorithm>
#include<string>
int main(){
const std::string strArray[] = {"an","blau","Bo","Boot","bos","da","Fee","fern","Fest","fort","je","jemand","mir","Mix",
"Mixer","Name","neu","od","Ort","so","Tor","Torf","Wasser"};
std::string value = "JNQ";
for_each(value.begin(), value.end(), [strArray](char c){
std::string const * iterator = find_if(strArray, strArray+23, [c](std::string str){
return toupper(str[0]) == c;
});
std::cout<<*iterator<<'\n';
});
return 0;
}
我得到這樣的輸出:
je
Name
an
爲什麼「的」顯示? 我在Ubuntu上使用g ++ 4.5。
然後找到正確的方法是什麼? – badmaash
@Abhi:'if(iterator!= strArray + 23){std :: cout << * itrerator; }其他{std :: cout <<「錯誤」; }' - > http://ideone.com/Vh16L –
嗯。似乎工作。如果find_if只是返回null,會不會更好? – badmaash