2012-11-27 77 views
0

我該怎麼做? 整個一天,我一直在到處找一個解決辦法:( 它在C++中,所有的問題是在標題,謝謝對一個字符串進行迭代並與另一個字符串進行比較以檢查第一個字符是否在第二個字符中

if(fp.is_open()) 
{ 
    while(getline(fp, buff)) 
    { 
     if(buff.length() == lung) 
     { 
      // check if the characters are in the string 

      while(found != string::npos) 
      { 
       cout << i << "\r"; 

       for(int x = 0; x < buff.length(); ++x) 
       { 
        found = lettere_possibili.find(buff[x]); 

        if(found == string::npos) 
        { 
         continue; 
        } 
       } 

       ++i;   
      } 

      j = 0; 
     } 

     ++k; 
    } 

    fp.close(); 
} 
+1

你只是想比較兩個字符串以顯示它們是否完全相同? – PaulG

+1

如果一個字符串是第二個字符串的* substring *,你在看嗎?哪一個是其中的一個子串是重要的? (即輸入'(「aa」,「aaa」)'將產生與「(」aaa「,」aa「)相同的答案) – amit

+3

所以你檢查一個是否是另一個的字謎?我只是將它們排序並檢查是否平等。 – chris

回答

5

的問題是不完全清楚,但在我看來,因爲如果你正在尋找find_first_not_of()

bool containsOnlyCharsFromPossibili = (buff.find_first_not_of(lettere_possibili) == string::npos); 
+0

謝謝sooooo,今天早上我一直在努力解決問題,我愛你<3 – DomeWTF

+0

@DomenicoMastrangelo如果答案爲你解決了問題,接受它如何? – Angew

0
#include <iostream> 

void find_chars(const std::string &first, const std::string &second) 
{ 
    std::string::const_iterator s; 

    for (s = second.begin(); s != second.end(); ++s) 
    { 
     if (first.find(*s) != std::string::npos) 
     { 
      std::cout << *s << " is in " << second << "\n"; 
     } 
    } 
} 

int main() 
{ 
    find_chars("abc", "abcdef"); 
} 

下面是輸出:

a is in abcdef 
b is in abcdef 
c is in abcdef 
+0

我已經這樣做了,但我無法讓程序遍歷一個單詞,如果它包含所有你正在搜索的字符,並且它的長度如何你想要它,顯示它在命令行,只有比去下一個字 – DomeWTF

+0

,如果它沒有在它的所有字符,如果第一個字符串中的字符不是第二 爲前: pappagalloverk pappagallini i和n不是「pappagalloverk」和所以它應該只是去下一個 – DomeWTF

相關問題