في C++، السلسلة هي كائن الأمراض المنقولة جنسيا::سلسلة فئة تمثل تسلسل الأحرف. يمكننا إجراء العديد من العمليات على السلاسل مثل التسلسل والمقارنة والتحويل وما إلى ذلك.
مثال سلسلة C++
دعونا نرى المثال البسيط لسلسلة C++.
#include using namespace std; int main( ) { string s1 = 'Hello'; char ch[] = { 'C', '+', '+'}; string s2 = string(ch); cout<<s1<<endl; cout<<s2<<endl; } < pre> <p>Output:</p> <pre> Hello C++ </pre> <hr> <h2>C++ String Compare Example</h2> <p>Let's see the simple example of string comparison using strcmp() function.</p> <pre> #include #include using namespace std; int main () { char key[] = 'mango'; char buffer[50]; do { cout<>buffer; } while (strcmp (key,buffer) != 0); cout<<'answer is correct!!'<<endl; return 0; } < pre> <p>Output:</p> <pre> What is my favourite fruit? apple What is my favourite fruit? banana What is my favourite fruit? mango Answer is correct!! </pre> <hr> <h2>C++ String Concat Example</h2> <p>Let's see the simple example of string concatenation using strcat() function.</p> <pre> #include #include using namespace std; int main() { char key[25], buffer[25]; cout << 'Enter the key string: '; cin.getline(key, 25); cout << 'Enter the buffer string: '; cin.getline(buffer, 25); strcat(key, buffer); cout << 'Key = ' << key << endl; cout << 'Buffer = ' << buffer<<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: Welcome to Enter the buffer string: C++ Programming. Key = Welcome to C++ Programming. Buffer = C++ Programming. </pre> <hr> <h2>C++ String Copy Example</h2> <p>Let's see the simple example of copy the string using strcpy() function.</p> <pre> #include #include using namespace std; int main() { char key[25], buffer[25]; cout << 'Enter the key string: '; cin.getline(key, 25); strcpy(buffer, key); cout << 'Key = '<< key << endl; cout << 'Buffer = '<< buffer<<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: C++ Tutorial Key = C++ Tutorial Buffer = C++ Tutorial </pre> <hr> <h2>C++ String Length Example</h2> <p>Let's see the simple example of finding the string length using strlen() function.</p> <pre> #include #include using namespace std; int main() { char ary[] = 'Welcome to C++ Programming'; cout << 'Length of String = ' << strlen(ary)<<endl; return 0; } < pre> <p>Output:</p> <pre> Length of String = 26 </pre> <h2>C++ String Functions</h2> <table class="table"> <tr> <th>Function</th> <th>Description</th> </tr> <tr> <td> int compare(const string& str) </td> <td>It is used to compare two string objects.</td> </tr> <tr> <td> <a href="/c-string-length">int length()</a> </td> <td>It is used to find the length of the string.</td> </tr> <tr> <td> void swap(string& str) </td> <td>It is used to swap the values of two string objects.</td> </tr> <tr> <td>string substr(int pos,int n)</td> <td>It creates a new string object of n characters.</td> </tr> <tr> <td> <a href="/c-string-size">int size()</a> </td> <td>It returns the length of the string in terms of bytes.</td> </tr> <tr> <td> void resize(int n) </td> <td>It is used to resize the length of the string up to n characters.</td> </tr> <tr> <td> <a href="/c-string-replace">string& replace(int pos,int len,string& str)</a> </td> <td>It replaces portion of the string that begins at character position pos and spans len characters.</td> </tr> <tr> <td> <a href="/c-string-append">string& append(const string& str)</a> </td> <td>It adds new characters at the end of another string object.</td> </tr> <tr> <td> char& at(int pos) </td> <td>It is used to access an individual character at specified position pos.</td> </tr> <tr> <td> <a href="/c-string-find">int find(string& str,int pos,int n)</a> </td> <td>It is used to find the string specified in the parameter.</td> </tr> <tr> <td> int find_first_of(string& str,int pos,int n) </td> <td>It is used to find the first occurrence of the specified sequence.</td> </tr> <tr> <td> int find_first_not_of(string& str,int pos,int n ) </td> <td>It is used to search the string for the first character that does not match with any of the characters specified in the string.</td> </tr> <tr> <td> int find_last_of(string& str,int pos,int n) </td> <td>It is used to search the string for the last character of specified sequence.</td> </tr> <tr> <td> int find_last_not_of(string& str,int pos) </td> <td>It searches for the last character that does not match with the specified sequence.</td> </tr> <tr> <td> string& insert() </td> <td>It inserts a new character before the character indicated by the position pos.</td> </tr> <tr> <td> int max_size() </td> <td>It finds the maximum length of the string.</td> </tr> <tr> <td> <a href="/c-string-push_back">void push_back(char ch)</a> </td> <td>It adds a new character ch at the end of the string.</td> </tr> <tr> <td> void pop_back() </td> <td>It removes a last character of the string.</td> </tr> <tr> <td> <a href="/c-string-assign">string& assign()</a> </td> <td>It assigns new value to the string.</td> </tr> <tr> <td> int copy(string& str) </td> <td>It copies the contents of string into another.</td> </tr> <tr> <td> char& back() </td> <td>It returns the reference of last character.</td> </tr> <tr> <td> Iterator begin() </td> <td>It returns the reference of first character.</td> </tr> <tr> <td> int capacity() </td> <td>It returns the allocated space for the string.</td> </tr> <tr> <td>const_iterator cbegin()</td> <td>It points to the first element of the string.</td> </tr> <tr> <td>const_iterator cend()</td> <td>It points to the last element of the string.</td> </tr> <tr> <td>void clear()</td> <td>It removes all the elements from the string.</td> </tr> <tr> <td>const_reverse_iterator crbegin()</td> <td>It points to the last character of the string.</td> </tr> <tr> <td>const_char* data()</td> <td>It copies the characters of string into an array.</td> </tr> <tr> <td>bool empty()</td> <td>It checks whether the string is empty or not.</td> </tr> <tr> <td>string& erase()</td> <td>It removes the characters as specified.</td> </tr> <tr> <td> char& front() </td> <td>It returns a reference of the first character.</td> </tr> <tr> <td> string&� operator+=() </td> <td>It appends a new character at the end of the string.</td> </tr> <tr> <td> string& operator=() </td> <td>It assigns a new value to the string.</td> </tr> <tr> <td>char operator[](pos)</td> <td>It retrieves a character at specified position pos.</td> </tr> <tr> <td> int rfind() </td> <td>It searches for the last occurrence of the string.</td> </tr> <tr> <td> iterator end() </td> <td>It references the last character of the string.</td> </tr> <tr> <td> reverse_iterator rend() </td> <td>It points to the first character of the string.</td> </tr> <tr> <td> void shrink_to_fit() </td> <td>It reduces the capacity and makes it equal to the size of the string.</td> </tr> <tr> <td> char* c_str() </td> <td>It returns pointer to an array that contains null terminated sequence of characters.</td> </tr> <tr> <td> const_reverse_iterator crend() </td> <td>It references the first character of the string.</td> </tr> <tr> <td> reverse_iterator rbegin() </td> <td>It reference the last character of the string.</td> </tr> <tr> <td>void reserve(inr len)</td> <td>It requests a change in capacity.</td> </tr> <tr> <td>allocator_type get_allocator();</td> <td>It returns the allocated object associated with the string.</td> </tr> </table></endl;></pre></endl;></pre></endl;></pre></'answer></pre></s1<<endl;>
مثال على مقارنة سلسلة C++
دعونا نرى المثال البسيط لمقارنة السلسلة باستخدام الدالة strcmp().
#include #include using namespace std; int main () { char key[] = 'mango'; char buffer[50]; do { cout<>buffer; } while (strcmp (key,buffer) != 0); cout<<\'answer is correct!!\'<<endl; return 0; } < pre> <p>Output:</p> <pre> What is my favourite fruit? apple What is my favourite fruit? banana What is my favourite fruit? mango Answer is correct!! </pre> <hr> <h2>C++ String Concat Example</h2> <p>Let's see the simple example of string concatenation using strcat() function.</p> <pre> #include #include using namespace std; int main() { char key[25], buffer[25]; cout << 'Enter the key string: '; cin.getline(key, 25); cout << 'Enter the buffer string: '; cin.getline(buffer, 25); strcat(key, buffer); cout << 'Key = ' << key << endl; cout << 'Buffer = ' << buffer<<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: Welcome to Enter the buffer string: C++ Programming. Key = Welcome to C++ Programming. Buffer = C++ Programming. </pre> <hr> <h2>C++ String Copy Example</h2> <p>Let's see the simple example of copy the string using strcpy() function.</p> <pre> #include #include using namespace std; int main() { char key[25], buffer[25]; cout << 'Enter the key string: '; cin.getline(key, 25); strcpy(buffer, key); cout << 'Key = '<< key << endl; cout << 'Buffer = '<< buffer<<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: C++ Tutorial Key = C++ Tutorial Buffer = C++ Tutorial </pre> <hr> <h2>C++ String Length Example</h2> <p>Let's see the simple example of finding the string length using strlen() function.</p> <pre> #include #include using namespace std; int main() { char ary[] = 'Welcome to C++ Programming'; cout << 'Length of String = ' << strlen(ary)<<endl; return 0; } < pre> <p>Output:</p> <pre> Length of String = 26 </pre> <h2>C++ String Functions</h2> <table class="table"> <tr> <th>Function</th> <th>Description</th> </tr> <tr> <td> int compare(const string& str) </td> <td>It is used to compare two string objects.</td> </tr> <tr> <td> <a href="/c-string-length">int length()</a> </td> <td>It is used to find the length of the string.</td> </tr> <tr> <td> void swap(string& str) </td> <td>It is used to swap the values of two string objects.</td> </tr> <tr> <td>string substr(int pos,int n)</td> <td>It creates a new string object of n characters.</td> </tr> <tr> <td> <a href="/c-string-size">int size()</a> </td> <td>It returns the length of the string in terms of bytes.</td> </tr> <tr> <td> void resize(int n) </td> <td>It is used to resize the length of the string up to n characters.</td> </tr> <tr> <td> <a href="/c-string-replace">string& replace(int pos,int len,string& str)</a> </td> <td>It replaces portion of the string that begins at character position pos and spans len characters.</td> </tr> <tr> <td> <a href="/c-string-append">string& append(const string& str)</a> </td> <td>It adds new characters at the end of another string object.</td> </tr> <tr> <td> char& at(int pos) </td> <td>It is used to access an individual character at specified position pos.</td> </tr> <tr> <td> <a href="/c-string-find">int find(string& str,int pos,int n)</a> </td> <td>It is used to find the string specified in the parameter.</td> </tr> <tr> <td> int find_first_of(string& str,int pos,int n) </td> <td>It is used to find the first occurrence of the specified sequence.</td> </tr> <tr> <td> int find_first_not_of(string& str,int pos,int n ) </td> <td>It is used to search the string for the first character that does not match with any of the characters specified in the string.</td> </tr> <tr> <td> int find_last_of(string& str,int pos,int n) </td> <td>It is used to search the string for the last character of specified sequence.</td> </tr> <tr> <td> int find_last_not_of(string& str,int pos) </td> <td>It searches for the last character that does not match with the specified sequence.</td> </tr> <tr> <td> string& insert() </td> <td>It inserts a new character before the character indicated by the position pos.</td> </tr> <tr> <td> int max_size() </td> <td>It finds the maximum length of the string.</td> </tr> <tr> <td> <a href="/c-string-push_back">void push_back(char ch)</a> </td> <td>It adds a new character ch at the end of the string.</td> </tr> <tr> <td> void pop_back() </td> <td>It removes a last character of the string.</td> </tr> <tr> <td> <a href="/c-string-assign">string& assign()</a> </td> <td>It assigns new value to the string.</td> </tr> <tr> <td> int copy(string& str) </td> <td>It copies the contents of string into another.</td> </tr> <tr> <td> char& back() </td> <td>It returns the reference of last character.</td> </tr> <tr> <td> Iterator begin() </td> <td>It returns the reference of first character.</td> </tr> <tr> <td> int capacity() </td> <td>It returns the allocated space for the string.</td> </tr> <tr> <td>const_iterator cbegin()</td> <td>It points to the first element of the string.</td> </tr> <tr> <td>const_iterator cend()</td> <td>It points to the last element of the string.</td> </tr> <tr> <td>void clear()</td> <td>It removes all the elements from the string.</td> </tr> <tr> <td>const_reverse_iterator crbegin()</td> <td>It points to the last character of the string.</td> </tr> <tr> <td>const_char* data()</td> <td>It copies the characters of string into an array.</td> </tr> <tr> <td>bool empty()</td> <td>It checks whether the string is empty or not.</td> </tr> <tr> <td>string& erase()</td> <td>It removes the characters as specified.</td> </tr> <tr> <td> char& front() </td> <td>It returns a reference of the first character.</td> </tr> <tr> <td> string&� operator+=() </td> <td>It appends a new character at the end of the string.</td> </tr> <tr> <td> string& operator=() </td> <td>It assigns a new value to the string.</td> </tr> <tr> <td>char operator[](pos)</td> <td>It retrieves a character at specified position pos.</td> </tr> <tr> <td> int rfind() </td> <td>It searches for the last occurrence of the string.</td> </tr> <tr> <td> iterator end() </td> <td>It references the last character of the string.</td> </tr> <tr> <td> reverse_iterator rend() </td> <td>It points to the first character of the string.</td> </tr> <tr> <td> void shrink_to_fit() </td> <td>It reduces the capacity and makes it equal to the size of the string.</td> </tr> <tr> <td> char* c_str() </td> <td>It returns pointer to an array that contains null terminated sequence of characters.</td> </tr> <tr> <td> const_reverse_iterator crend() </td> <td>It references the first character of the string.</td> </tr> <tr> <td> reverse_iterator rbegin() </td> <td>It reference the last character of the string.</td> </tr> <tr> <td>void reserve(inr len)</td> <td>It requests a change in capacity.</td> </tr> <tr> <td>allocator_type get_allocator();</td> <td>It returns the allocated object associated with the string.</td> </tr> </table></endl;></pre></endl;></pre></endl;></pre></\'answer>
مثال على سلسلة C++
دعونا نرى المثال البسيط لتسلسل السلسلة باستخدام الدالة strcat().
قوائم المغلق
#include #include using namespace std; int main() { char key[25], buffer[25]; cout << 'Enter the key string: '; cin.getline(key, 25); cout << 'Enter the buffer string: '; cin.getline(buffer, 25); strcat(key, buffer); cout << 'Key = ' << key << endl; cout << 'Buffer = ' << buffer<<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: Welcome to Enter the buffer string: C++ Programming. Key = Welcome to C++ Programming. Buffer = C++ Programming. </pre> <hr> <h2>C++ String Copy Example</h2> <p>Let's see the simple example of copy the string using strcpy() function.</p> <pre> #include #include using namespace std; int main() { char key[25], buffer[25]; cout << 'Enter the key string: '; cin.getline(key, 25); strcpy(buffer, key); cout << 'Key = '<< key << endl; cout << 'Buffer = '<< buffer<<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: C++ Tutorial Key = C++ Tutorial Buffer = C++ Tutorial </pre> <hr> <h2>C++ String Length Example</h2> <p>Let's see the simple example of finding the string length using strlen() function.</p> <pre> #include #include using namespace std; int main() { char ary[] = 'Welcome to C++ Programming'; cout << 'Length of String = ' << strlen(ary)<<endl; return 0; } < pre> <p>Output:</p> <pre> Length of String = 26 </pre> <h2>C++ String Functions</h2> <table class="table"> <tr> <th>Function</th> <th>Description</th> </tr> <tr> <td> int compare(const string& str) </td> <td>It is used to compare two string objects.</td> </tr> <tr> <td> <a href="/c-string-length">int length()</a> </td> <td>It is used to find the length of the string.</td> </tr> <tr> <td> void swap(string& str) </td> <td>It is used to swap the values of two string objects.</td> </tr> <tr> <td>string substr(int pos,int n)</td> <td>It creates a new string object of n characters.</td> </tr> <tr> <td> <a href="/c-string-size">int size()</a> </td> <td>It returns the length of the string in terms of bytes.</td> </tr> <tr> <td> void resize(int n) </td> <td>It is used to resize the length of the string up to n characters.</td> </tr> <tr> <td> <a href="/c-string-replace">string& replace(int pos,int len,string& str)</a> </td> <td>It replaces portion of the string that begins at character position pos and spans len characters.</td> </tr> <tr> <td> <a href="/c-string-append">string& append(const string& str)</a> </td> <td>It adds new characters at the end of another string object.</td> </tr> <tr> <td> char& at(int pos) </td> <td>It is used to access an individual character at specified position pos.</td> </tr> <tr> <td> <a href="/c-string-find">int find(string& str,int pos,int n)</a> </td> <td>It is used to find the string specified in the parameter.</td> </tr> <tr> <td> int find_first_of(string& str,int pos,int n) </td> <td>It is used to find the first occurrence of the specified sequence.</td> </tr> <tr> <td> int find_first_not_of(string& str,int pos,int n ) </td> <td>It is used to search the string for the first character that does not match with any of the characters specified in the string.</td> </tr> <tr> <td> int find_last_of(string& str,int pos,int n) </td> <td>It is used to search the string for the last character of specified sequence.</td> </tr> <tr> <td> int find_last_not_of(string& str,int pos) </td> <td>It searches for the last character that does not match with the specified sequence.</td> </tr> <tr> <td> string& insert() </td> <td>It inserts a new character before the character indicated by the position pos.</td> </tr> <tr> <td> int max_size() </td> <td>It finds the maximum length of the string.</td> </tr> <tr> <td> <a href="/c-string-push_back">void push_back(char ch)</a> </td> <td>It adds a new character ch at the end of the string.</td> </tr> <tr> <td> void pop_back() </td> <td>It removes a last character of the string.</td> </tr> <tr> <td> <a href="/c-string-assign">string& assign()</a> </td> <td>It assigns new value to the string.</td> </tr> <tr> <td> int copy(string& str) </td> <td>It copies the contents of string into another.</td> </tr> <tr> <td> char& back() </td> <td>It returns the reference of last character.</td> </tr> <tr> <td> Iterator begin() </td> <td>It returns the reference of first character.</td> </tr> <tr> <td> int capacity() </td> <td>It returns the allocated space for the string.</td> </tr> <tr> <td>const_iterator cbegin()</td> <td>It points to the first element of the string.</td> </tr> <tr> <td>const_iterator cend()</td> <td>It points to the last element of the string.</td> </tr> <tr> <td>void clear()</td> <td>It removes all the elements from the string.</td> </tr> <tr> <td>const_reverse_iterator crbegin()</td> <td>It points to the last character of the string.</td> </tr> <tr> <td>const_char* data()</td> <td>It copies the characters of string into an array.</td> </tr> <tr> <td>bool empty()</td> <td>It checks whether the string is empty or not.</td> </tr> <tr> <td>string& erase()</td> <td>It removes the characters as specified.</td> </tr> <tr> <td> char& front() </td> <td>It returns a reference of the first character.</td> </tr> <tr> <td> string&� operator+=() </td> <td>It appends a new character at the end of the string.</td> </tr> <tr> <td> string& operator=() </td> <td>It assigns a new value to the string.</td> </tr> <tr> <td>char operator[](pos)</td> <td>It retrieves a character at specified position pos.</td> </tr> <tr> <td> int rfind() </td> <td>It searches for the last occurrence of the string.</td> </tr> <tr> <td> iterator end() </td> <td>It references the last character of the string.</td> </tr> <tr> <td> reverse_iterator rend() </td> <td>It points to the first character of the string.</td> </tr> <tr> <td> void shrink_to_fit() </td> <td>It reduces the capacity and makes it equal to the size of the string.</td> </tr> <tr> <td> char* c_str() </td> <td>It returns pointer to an array that contains null terminated sequence of characters.</td> </tr> <tr> <td> const_reverse_iterator crend() </td> <td>It references the first character of the string.</td> </tr> <tr> <td> reverse_iterator rbegin() </td> <td>It reference the last character of the string.</td> </tr> <tr> <td>void reserve(inr len)</td> <td>It requests a change in capacity.</td> </tr> <tr> <td>allocator_type get_allocator();</td> <td>It returns the allocated object associated with the string.</td> </tr> </table></endl;></pre></endl;></pre></endl;>
مثال على نسخ سلسلة C++
دعونا نرى المثال البسيط لنسخ السلسلة باستخدام الدالة strcpy().
#include #include using namespace std; int main() { char key[25], buffer[25]; cout << 'Enter the key string: '; cin.getline(key, 25); strcpy(buffer, key); cout << 'Key = '<< key << endl; cout << 'Buffer = '<< buffer<<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: C++ Tutorial Key = C++ Tutorial Buffer = C++ Tutorial </pre> <hr> <h2>C++ String Length Example</h2> <p>Let's see the simple example of finding the string length using strlen() function.</p> <pre> #include #include using namespace std; int main() { char ary[] = 'Welcome to C++ Programming'; cout << 'Length of String = ' << strlen(ary)<<endl; return 0; } < pre> <p>Output:</p> <pre> Length of String = 26 </pre> <h2>C++ String Functions</h2> <table class="table"> <tr> <th>Function</th> <th>Description</th> </tr> <tr> <td> int compare(const string& str) </td> <td>It is used to compare two string objects.</td> </tr> <tr> <td> <a href="/c-string-length">int length()</a> </td> <td>It is used to find the length of the string.</td> </tr> <tr> <td> void swap(string& str) </td> <td>It is used to swap the values of two string objects.</td> </tr> <tr> <td>string substr(int pos,int n)</td> <td>It creates a new string object of n characters.</td> </tr> <tr> <td> <a href="/c-string-size">int size()</a> </td> <td>It returns the length of the string in terms of bytes.</td> </tr> <tr> <td> void resize(int n) </td> <td>It is used to resize the length of the string up to n characters.</td> </tr> <tr> <td> <a href="/c-string-replace">string& replace(int pos,int len,string& str)</a> </td> <td>It replaces portion of the string that begins at character position pos and spans len characters.</td> </tr> <tr> <td> <a href="/c-string-append">string& append(const string& str)</a> </td> <td>It adds new characters at the end of another string object.</td> </tr> <tr> <td> char& at(int pos) </td> <td>It is used to access an individual character at specified position pos.</td> </tr> <tr> <td> <a href="/c-string-find">int find(string& str,int pos,int n)</a> </td> <td>It is used to find the string specified in the parameter.</td> </tr> <tr> <td> int find_first_of(string& str,int pos,int n) </td> <td>It is used to find the first occurrence of the specified sequence.</td> </tr> <tr> <td> int find_first_not_of(string& str,int pos,int n ) </td> <td>It is used to search the string for the first character that does not match with any of the characters specified in the string.</td> </tr> <tr> <td> int find_last_of(string& str,int pos,int n) </td> <td>It is used to search the string for the last character of specified sequence.</td> </tr> <tr> <td> int find_last_not_of(string& str,int pos) </td> <td>It searches for the last character that does not match with the specified sequence.</td> </tr> <tr> <td> string& insert() </td> <td>It inserts a new character before the character indicated by the position pos.</td> </tr> <tr> <td> int max_size() </td> <td>It finds the maximum length of the string.</td> </tr> <tr> <td> <a href="/c-string-push_back">void push_back(char ch)</a> </td> <td>It adds a new character ch at the end of the string.</td> </tr> <tr> <td> void pop_back() </td> <td>It removes a last character of the string.</td> </tr> <tr> <td> <a href="/c-string-assign">string& assign()</a> </td> <td>It assigns new value to the string.</td> </tr> <tr> <td> int copy(string& str) </td> <td>It copies the contents of string into another.</td> </tr> <tr> <td> char& back() </td> <td>It returns the reference of last character.</td> </tr> <tr> <td> Iterator begin() </td> <td>It returns the reference of first character.</td> </tr> <tr> <td> int capacity() </td> <td>It returns the allocated space for the string.</td> </tr> <tr> <td>const_iterator cbegin()</td> <td>It points to the first element of the string.</td> </tr> <tr> <td>const_iterator cend()</td> <td>It points to the last element of the string.</td> </tr> <tr> <td>void clear()</td> <td>It removes all the elements from the string.</td> </tr> <tr> <td>const_reverse_iterator crbegin()</td> <td>It points to the last character of the string.</td> </tr> <tr> <td>const_char* data()</td> <td>It copies the characters of string into an array.</td> </tr> <tr> <td>bool empty()</td> <td>It checks whether the string is empty or not.</td> </tr> <tr> <td>string& erase()</td> <td>It removes the characters as specified.</td> </tr> <tr> <td> char& front() </td> <td>It returns a reference of the first character.</td> </tr> <tr> <td> string&� operator+=() </td> <td>It appends a new character at the end of the string.</td> </tr> <tr> <td> string& operator=() </td> <td>It assigns a new value to the string.</td> </tr> <tr> <td>char operator[](pos)</td> <td>It retrieves a character at specified position pos.</td> </tr> <tr> <td> int rfind() </td> <td>It searches for the last occurrence of the string.</td> </tr> <tr> <td> iterator end() </td> <td>It references the last character of the string.</td> </tr> <tr> <td> reverse_iterator rend() </td> <td>It points to the first character of the string.</td> </tr> <tr> <td> void shrink_to_fit() </td> <td>It reduces the capacity and makes it equal to the size of the string.</td> </tr> <tr> <td> char* c_str() </td> <td>It returns pointer to an array that contains null terminated sequence of characters.</td> </tr> <tr> <td> const_reverse_iterator crend() </td> <td>It references the first character of the string.</td> </tr> <tr> <td> reverse_iterator rbegin() </td> <td>It reference the last character of the string.</td> </tr> <tr> <td>void reserve(inr len)</td> <td>It requests a change in capacity.</td> </tr> <tr> <td>allocator_type get_allocator();</td> <td>It returns the allocated object associated with the string.</td> </tr> </table></endl;></pre></endl;>
مثال على طول سلسلة C++
دعونا نرى المثال البسيط لإيجاد طول السلسلة باستخدام الدالة strlen().
#include #include using namespace std; int main() { char ary[] = 'Welcome to C++ Programming'; cout << 'Length of String = ' << strlen(ary)<<endl; return 0; } < pre> <p>Output:</p> <pre> Length of String = 26 </pre> <h2>C++ String Functions</h2> <table class="table"> <tr> <th>Function</th> <th>Description</th> </tr> <tr> <td> int compare(const string& str) </td> <td>It is used to compare two string objects.</td> </tr> <tr> <td> <a href="/c-string-length">int length()</a> </td> <td>It is used to find the length of the string.</td> </tr> <tr> <td> void swap(string& str) </td> <td>It is used to swap the values of two string objects.</td> </tr> <tr> <td>string substr(int pos,int n)</td> <td>It creates a new string object of n characters.</td> </tr> <tr> <td> <a href="/c-string-size">int size()</a> </td> <td>It returns the length of the string in terms of bytes.</td> </tr> <tr> <td> void resize(int n) </td> <td>It is used to resize the length of the string up to n characters.</td> </tr> <tr> <td> <a href="/c-string-replace">string& replace(int pos,int len,string& str)</a> </td> <td>It replaces portion of the string that begins at character position pos and spans len characters.</td> </tr> <tr> <td> <a href="/c-string-append">string& append(const string& str)</a> </td> <td>It adds new characters at the end of another string object.</td> </tr> <tr> <td> char& at(int pos) </td> <td>It is used to access an individual character at specified position pos.</td> </tr> <tr> <td> <a href="/c-string-find">int find(string& str,int pos,int n)</a> </td> <td>It is used to find the string specified in the parameter.</td> </tr> <tr> <td> int find_first_of(string& str,int pos,int n) </td> <td>It is used to find the first occurrence of the specified sequence.</td> </tr> <tr> <td> int find_first_not_of(string& str,int pos,int n ) </td> <td>It is used to search the string for the first character that does not match with any of the characters specified in the string.</td> </tr> <tr> <td> int find_last_of(string& str,int pos,int n) </td> <td>It is used to search the string for the last character of specified sequence.</td> </tr> <tr> <td> int find_last_not_of(string& str,int pos) </td> <td>It searches for the last character that does not match with the specified sequence.</td> </tr> <tr> <td> string& insert() </td> <td>It inserts a new character before the character indicated by the position pos.</td> </tr> <tr> <td> int max_size() </td> <td>It finds the maximum length of the string.</td> </tr> <tr> <td> <a href="/c-string-push_back">void push_back(char ch)</a> </td> <td>It adds a new character ch at the end of the string.</td> </tr> <tr> <td> void pop_back() </td> <td>It removes a last character of the string.</td> </tr> <tr> <td> <a href="/c-string-assign">string& assign()</a> </td> <td>It assigns new value to the string.</td> </tr> <tr> <td> int copy(string& str) </td> <td>It copies the contents of string into another.</td> </tr> <tr> <td> char& back() </td> <td>It returns the reference of last character.</td> </tr> <tr> <td> Iterator begin() </td> <td>It returns the reference of first character.</td> </tr> <tr> <td> int capacity() </td> <td>It returns the allocated space for the string.</td> </tr> <tr> <td>const_iterator cbegin()</td> <td>It points to the first element of the string.</td> </tr> <tr> <td>const_iterator cend()</td> <td>It points to the last element of the string.</td> </tr> <tr> <td>void clear()</td> <td>It removes all the elements from the string.</td> </tr> <tr> <td>const_reverse_iterator crbegin()</td> <td>It points to the last character of the string.</td> </tr> <tr> <td>const_char* data()</td> <td>It copies the characters of string into an array.</td> </tr> <tr> <td>bool empty()</td> <td>It checks whether the string is empty or not.</td> </tr> <tr> <td>string& erase()</td> <td>It removes the characters as specified.</td> </tr> <tr> <td> char& front() </td> <td>It returns a reference of the first character.</td> </tr> <tr> <td> string&� operator+=() </td> <td>It appends a new character at the end of the string.</td> </tr> <tr> <td> string& operator=() </td> <td>It assigns a new value to the string.</td> </tr> <tr> <td>char operator[](pos)</td> <td>It retrieves a character at specified position pos.</td> </tr> <tr> <td> int rfind() </td> <td>It searches for the last occurrence of the string.</td> </tr> <tr> <td> iterator end() </td> <td>It references the last character of the string.</td> </tr> <tr> <td> reverse_iterator rend() </td> <td>It points to the first character of the string.</td> </tr> <tr> <td> void shrink_to_fit() </td> <td>It reduces the capacity and makes it equal to the size of the string.</td> </tr> <tr> <td> char* c_str() </td> <td>It returns pointer to an array that contains null terminated sequence of characters.</td> </tr> <tr> <td> const_reverse_iterator crend() </td> <td>It references the first character of the string.</td> </tr> <tr> <td> reverse_iterator rbegin() </td> <td>It reference the last character of the string.</td> </tr> <tr> <td>void reserve(inr len)</td> <td>It requests a change in capacity.</td> </tr> <tr> <td>allocator_type get_allocator();</td> <td>It returns the allocated object associated with the string.</td> </tr> </table></endl;>
وظائف سلسلة C ++
وظيفة | وصف |
---|---|
مقارنة int (سلسلة const & str) | يتم استخدامه لمقارنة كائنين من السلسلة. |
طول الباحث () | يتم استخدامه للعثور على طول السلسلة. |
مبادلة باطلة (سلسلة وسلسلة) | يتم استخدامه لمبادلة قيم كائنين من السلسلة. |
سلسلة فرعية (int pos، int n) | يقوم بإنشاء كائن سلسلة جديد من أحرف n. |
حجم صحيح () | تقوم بإرجاع طول السلسلة من حيث البايتات. |
تغيير حجم الفراغ (int n) | يتم استخدامه لتغيير حجم السلسلة حتى عدد n من الأحرف. |
سلسلة واستبدال (int pos، int len، string& str) | فهو يستبدل جزءًا من السلسلة التي تبدأ عند موضع الحرف pos ويمتد إلى أحرف len. |
سلسلة وإلحاق (سلسلة ثابتة وسلسلة) | يضيف أحرفًا جديدة في نهاية كائن سلسلة آخر. |
شار & في (int pos) | يتم استخدامه للوصول إلى شخصية فردية في موضع محدد. |
الباحث البحث (سلسلة & شارع، الباحث نقاط البيع، الباحث ن) | يتم استخدامه للعثور على السلسلة المحددة في المعلمة. |
int find_first_of(string& str,int pos,int n) | يتم استخدامه للعثور على التواجد الأول للتسلسل المحدد. |
int find_first_not_of(string& str,int pos,int n ) | يتم استخدامه للبحث في السلسلة عن الحرف الأول الذي لا يتطابق مع أي من الأحرف المحددة في السلسلة. |
int find_last_of(string& str,int pos,int n) | يتم استخدامه للبحث في السلسلة عن الحرف الأخير من التسلسل المحدد. |
int find_last_not_of(string& str,int pos) | يبحث عن الحرف الأخير الذي لا يتطابق مع التسلسل المحدد. |
سلسلة وإدراج () | يقوم بإدراج حرف جديد قبل الحرف المشار إليه بواسطة الموضع pos. |
كثافة العمليات max_size() | يجد الحد الأقصى لطول السلسلة. |
باطلة Push_back (شار الفصل) | يضيف حرفًا جديدًا ch في نهاية السلسلة. |
باطلة pop_back() | يقوم بإزالة الحرف الأخير من السلسلة. |
سلسلة وتعيين () | يقوم بتعيين قيمة جديدة للسلسلة. |
نسخة int (سلسلة وسلسلة) | يقوم بنسخ محتويات السلسلة إلى أخرى. |
شار & العودة () | تقوم بإرجاع مرجع الحرف الأخير. |
يبدأ المكرر () | تقوم بإرجاع مرجع الحرف الأول. |
سعة كثافة العمليات () | تقوم بإرجاع المساحة المخصصة للسلسلة. |
const_iterator cbegin() | ويشير إلى العنصر الأول من السلسلة. |
const_iterator cend() | يشير إلى العنصر الأخير في السلسلة. |
باطل واضح () | يقوم بإزالة كافة العناصر من السلسلة. |
const_reverse_iterator crbegin() | ويشير إلى الحرف الأخير من السلسلة. |
const_char* البيانات () | يقوم بنسخ أحرف السلسلة إلى صفيف. |
منطقي فارغ () | يتحقق ما إذا كانت السلسلة فارغة أم لا. |
سلسلة ومسح () | يقوم بإزالة الأحرف كما هو محدد. |
شار & الجبهة () | تقوم بإرجاع مرجع الحرف الأول. |
عامل السلسلة&+=() | يقوم بإلحاق حرف جديد في نهاية السلسلة. |
سلسلة وعامل = () | يقوم بتعيين قيمة جديدة للسلسلة. |
عامل شار[](بوس) | يقوم باسترداد حرف في الموضع المحدد. |
إنت رفايند () | يبحث عن التواجد الأخير للسلسلة. |
نهاية المكرر () | يشير إلى الحرف الأخير من السلسلة. |
عكس_المكرر ريند () | ويشير إلى الحرف الأول من السلسلة. |
باطل يتقلص_to_fit() | فهو يقلل من السعة ويجعلها مساوية لحجم السلسلة. |
شار* c_str() | يقوم بإرجاع المؤشر إلى صفيف يحتوي على تسلسل أحرف منتهية خالية. |
const_reverse_iterator crend() | يشير إلى الحرف الأول من السلسلة. |
عكس_المكرر rbegin () | تشير إلى الحرف الأخير من السلسلة. |
احتياطي الفراغ (لين لين) | ويطلب تغيير في القدرة. |
allocator_type get_allocator(); | تقوم بإرجاع الكائن المخصص المرتبط بالسلسلة. |