2017-04-15 161 views
0

我需要幫助解決我的問題。我有一個字符串數組,它是string * A如何比較C++中數組中字符串的一部分字符串?

A = { dog, doom, dorm, dunk, face, fall, falafel, fart } 

我需要檢查我需要多少字符串「d」,這是4
數組中檢查有多少串「做」,這是3

我到陣列中使用二進制搜索來找到它。我的問題是如何訪問數組中的字符串的一部分,並能夠與另一個字符串進行比較?

+0

你給僞代碼,並要求真正的代碼 - 我們怎麼知道? – Chad

+0

「我用二進制搜索找到它」 - 當你做* *時,你是如何「訪問」數組中的字符串的?有可能會是一個類似的努力。 – WhozCraig

+0

[check-if-a-string-contains-a-string](https://stackoverflow.com/questions/2340281/check-if-a-string-contains-a-string-in-c) – HDJEMAI

回答

0

在C++中的字符串數據類型,換句話說,字符數組(CHAR []),例如:

#include <conio.h> 
#include <string> 
#include <iostream> 

using namespace std; 


int main() { 
    //Declare the array 
    string foo[3]; 

    //Inserting values in the strings 
    foo[0] = "First word"; 
    foo[1] = "Second word"; 
    foo[2] = "Third word"; 

    //Show the first word of the first string 
    cout << foo[0][0]; 

    _getch(); 

    return 0; 
} 

因此可以與每個字符進行比較。