User Tools

Site Tools


cpp:strings

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
cpp:strings [2017/05/08 05:48] – [Μεταβολή του αλφαριθμητικού] gthanoscpp:strings [2017/05/08 06:00] – [Μεταβολή του αλφαριθμητικού] gthanos
Line 163: Line 163:
 iterator erase (iterator first, iterator last);</code>  | Διαγράφει μέρος του //string//, μειώνοντας το μήκος του.  | iterator erase (iterator first, iterator last);</code>  | Διαγράφει μέρος του //string//, μειώνοντας το μήκος του.  |
 |  <code cpp>string& replace (size_t pos,  size_t len,  const string& str); |  <code cpp>string& replace (size_t pos,  size_t len,  const string& str);
-string& replace (size_t pos,  size_t len,  const char* s);</code>  | Αντικαθιστά το υφιστάμενο //string// με το νέο //str// ξεκινώντας από την θέση //pos// του //string// και για μήκος //len//  | +string& replace (size_t pos,  size_t len,  const char* s); 
-|  <code cpp></code>   +string& replace (size_t pos,  size_t len,  const string& str, 
-|  <code cpp></code>  |   | +                 size_t subpos, size_t sublen); 
- <code cpp></code>    |+</code>  | Αντικαθιστά το υφιστάμενο //string// με το νέο //str// ξεκινώντας από την θέση //pos// του //string// και για μήκος //len//  | 
 +|  <code cpp>void swap (string& str);</code> Ανταλλάσει αμοιβαία το περιεχόμενο του τρέχοντος //string// με το περιοχόμενο του //string str//.  
 + 
 +<code cpp append.cpp> 
 +#include <iostream> 
 +#include <string> 
 +using namespace std; 
 + 
 +int main () { 
 +  string str="Hello"; 
 +  string str2=" "; 
 +  char[] str3="World"; 
 +  string str4 = "!\n"; 
 + 
 +  str+=str2; 
 +  str.append(str3); 
 +  str.append(str4); 
 +  cout << str; 
 +
 +</code>
  
 <code cpp replace.cpp> <code cpp replace.cpp>
Line 183: Line 202:
   str.replace(19,6,str3,7,6);     // "this is an example phrase." (2)   str.replace(19,6,str3,7,6);     // "this is an example phrase." (2)
   str.replace(8,10,"just a");     // "this is just a phrase."     (3)   str.replace(8,10,"just a");     // "this is just a phrase."     (3)
-  str.replace(8,6,"a shorty",7);  // "this is a short phrase."    (4) 
-  str.replace(22,1,3,'!');        // "this is a short phrase!!!"  (5) 
- 
-  // Using iterators:                                               0123456789*123456789* 
-  str.replace(str.begin(),str.end()-3,str3);                    // "sample phrase!!!"      (1) 
-  str.replace(str.begin(),str.begin()+6,"replace");             // "replace phrase!!!"     (3) 
-  str.replace(str.begin()+8,str.begin()+14,"is coolness",7);    // "replace is cool!!!"    (4) 
-  str.replace(str.begin()+12,str.end()-4,4,'o');                // "replace is cooool!!!"  (5) 
-  str.replace(str.begin()+11,str.end(),str4.begin(),str4.end());// "replace is useful."    (6) 
   cout << str << endl;   cout << str << endl;
 +}
 +</code>
 +
 +<code cpp chicken.cpp>
 +#include <iostream>
 +#include <string>
 +using namespace std;
 +
 +main (){
 +  string chicken ("chicken");
 +  string egg ("egg");
 +  cout << "Before the swap, chicken comes from " << chicken;
 +  cout << " and egg comes from " << egg << endl;
 +
 +  chicken.swap (egg);
 +  cout << "After the swap, chicken comes from " << chicken;
 +  cout << " and egg comes from " << egg << endl;
 } }
 </code> </code>
cpp/strings.txt · Last modified: 2021/04/27 04:45 (external edit)