Note: use the github provided TOC for navigaing.
string find()
is different than the array/vector find()
finction.
string find()
:
here we are finding the 1st and the 2nd occurence of sub-string.
int main(){
string paragraph = "We are learning about STL strings. STL string class is quite powerful";
string word;
getline(cin,word);
//find function
int index = paragraph.find(word);
if(index!=-1){
cout <<"first occ" <<index;
}
index = paragraph.find(word,index+1);
if(index!=-1){
cout<<"second occ "<<index <<endl;
}
return 0;
}
good source: https://www.geeksforgeeks.org/string-find-in-cpp/
The function returns the index of the first occurrence of sub-string, if the sub-string is not found it returns string::npos(string::npos is static member with value as the highest possible for the size_t data structure).
find()
in array/vector:int main(){
int arr[] = {1,2,3,4,5};
int n = 5;
int key = 4;
auto it = find(arr,arr+n,key); // returns the address of the key
int idx = it-arr; // if you substract the address by the
// base then you will get the idx of key
cout<<idx; // prints the index of key
// find any element is not present
if(idx==n){
cout<<"adsent";
}
else{
cout<<"present";
}
}
str.substr()
function:int main() {
string word = "hello";
cout<<word.substr(0,2);
}
>>> he