Input and output:

 

cin : Read formatted data from the standard input

 

#include<iostream>

 

cin.getline : Get a line from the stdin stream.

 

cout : Print formatted output to the standard

         output stream.

 

#include<iostream>

 

setw(int n) : set the width of the next output to n chars.

setprecision(int n) : set the number of decimal fractions of the next input to n chars.

fixed : 以固定小數而非指數方式輸出浮點值

 

#include<iomainp>

 

 

Example :

 

#include <iostream>

#include <string>

#include<iomanip>

using namespace std;

void main()

{

        double i;

        cin>>i;

        cout<<fixed<<setprecision(3)<<setw(10)<<i;

}

 

Input :

123.4568

Output :

   123.457

 

String manipulation(in C++):

c_str : Transfer a string to char array.

 

#include <string>

using namespace std;

const charT* c_str () const;

 

Example :

 

#include <string>

#include <stdio.h>

using namespace std;

void main()

{

  string text;

  text=”This is a test”;

  printf(“%s\n”,text.c_str());

}

 

Output :

 

This is a test

 

erase : Remove some chars from a string.

 

#include <string>

using namespace std;

basic_string& erase (size_type pos = 0, size_type n = npos);

 

Example :

 

#include <string>

#include <stdio.h>

using namespace std;

void main()

{

  string text;

  text="abcdefghi";

  text.erase(4,3);

  cout<<text<<endl;

}

 

Output :

 

abcdhi

 

compare : Compare two strings.

 

#include <string>

using namespace std;

int compare (const basic_string& str);

int compare(size_type, size_type, const basic_string&)

int compare(size_type, size_type, const basic_string&, size_type, size_type);

 

Example :

 

#include <iostream>

#include <string>

using namespace std;

void print(int result)

{

   if (result < 0)

      cout<<"s1 < s2"<<endl;

   if (result == 0)

      cout<<"s1 == s2"<<endl;

   if (result > 0)

      cout<<"s1 > s2"<<endl;

}

void main()

{

  string text;

  int result;

  text = "A jackhammer";

  result=text.compare(“jack”);

  print(result);

  result=text.compare(2,4,”jack”);

  print(result);

}

 

Output :

 

s1 < s2

s1 == s2

 

find : find the first occurance of a char or a substring in a string

rfind : find the last occurance of a char or a substring in a string

 

#include<string>

using namespace std;

size_type find(const basic_string&, size_type = 0);

size_type find(const charT*, size_type, size_type);

size_type find(const charT*, size_type = 0) const;

size_type find(charT, size_type = 0) const;;

 

Example1 :

 

#include <iostream>

#include <string>

using namespace std;

void main()

{

  string text;

  text = "Manners maketh man";

  cout<<text.find('k')<<endl;

  cout<<text.find('x')<<endl;

  if(text.find('x') == string::npos)

  {

          cout<< "Character not found" << endl;

  }

}

 

Output1 :

 

10

4294967295

Character not found

 

Example2 :

 

#include <iostream>

#include <string>

using namespace std;

int main()

{

  string text = "Smith had had the examiners had Bob’s approval.";

  string word = "had";

  int count = 0;

  for(int index = 0 ; (index = text.find(word, index)) != string::npos ;                     index += word.length(), count++)

    ;

  cout << "Your text contained "<< count << " occurrences of \""<< word  << "\"." << endl;

  return 0;

}

 

Output1 :

 

Your text contained 3 occurrences of "had".

 

find_first_of : Find the first occurance of a set of chars in a string.

find_first_not_of : Find the first occurance not in a set of chars in a string.

find_last_of : likewise

find_last_not_of : likewise

 

#include <string>

using namespace std;

size_type find_first_of(const basic_string& str, size_type pos = 0)

size_type find_first_of(const charT* s, size_type pos, size_type n);

size_type find_first_of(const charT* s, size_type pos = 0);

 

Example :

 

#include <iostream>

#include <string>

using namespace std;

void main()

{

  string text = " Smith had\"had\" the examiners had Bob's approval.";

  string separators = " ,.\"";

  int start = text.find_first_not_of(separators);

  int end = 0;

  while(start != string::npos)

  {

    end = text.find_first_of(separators, start + 1);

    if(end == string::npos)

      end = text.length();

    cout << text.substr(start, end - start)

         << endl;

    start = text.find_first_not_of(separators, end + 1);

  }

}

Output :

 

Smith

had

had

the

examiners

had

Bob's

Approval

 

insert : insert a substring into a string.

 

#include <string>

using namespace std;

basic_string& insert(size_type pos1, const basic_string& s);

basic_string& insert(size_type pos, const basic_string& s, size_type pos2 = 0, size_type n = npos);

 

Example :

 

#include <iostream>

#include <string>

using namespace std;

void main()

{

  string text, word;

  text = "This is a sample";

  word = "very good ";

  text.insert(10,word,5,5);

  cout<<text<<endl;

}

Output :

 

This is a good sample

 

length : Get the length of a string.

 

#include <string>

using namespace std;

size_type length() const;

 

Example :

 

#include <iostream>

#include <string>

using namespace std;

void main()

{

  string text;

  text = "This is a sample";

   cout<<"text = "<<text<<"\nlength of text = "<<text.length()<<endl;

}

 

Output :

 

text = This is a sample

length of text = 16

 

replace : replace a substring of a string into another string

 

#include <string>

using namespace std;

basic_string& replace(size_type pos, size_type n1, const basic_string& s);

basic_string& replace(size_type pos1, size_type n1, const basic_string& str, size_type pos2, size_type n2);

 

Example :

 

#include <iostream>

#include <string>

using namespace std;

void main()

{

  string text, word;

  text = "This is a very good sample";

  word = "bad";

  text.replace(10,9,word);

  cout<<text<<endl;

}

 

Output :

 

This is a bad sample

 

String manipulation(Compare of C and C++):

 

C

C++

char * strcat(char *, const char *);

string + string

char * strchr(const char *, int);

string.find()

char * strcpy(char *, const char *);

string = string

int strcmp(const char *, const char *);

string1.compare(int index, int len, string2)

size_t strlen(const char *);

string.length()

char * _strlwr(char *);

for(i = 0;i<string.length();i++) {

string[i].tolower(string[i]); }

char * _strupr(char *)

for(i = 0;i<string.length();i++) {

string[i].toupper(string[i]); }

char * strstr(const char *, const char *);

string1.find(string2)

char * strtok(char *, const char *);

string1.find_first_of(string2)

 
http://cs.stmarys.ca/~porter/csc/ref/c_cpp_strings.html
 

STL:

 

vector : A useful array like stuff.

 

#include <vector>

 

Example :

 

#include <vector>

#include <iostream>

using namespace std;

void main()

{

        int ia[] = {29,23,20,22,17,15,26,51,19,12,35,40}; 

        vector< int > vec( ia, ia+12 );

}

 

binary search : Doing binary search, the input should be sorted

 

#include <algorithm>

bool binary_search (ForwardIterator first, ForwardIterator last,

     const T & value [, Compare ] );

 

Example :

 

#include <algorithm>

#include <vector>

#include <functional>

#include <iostream>

using namespace std;

void main()

{

        int ia[] = {29,23,20,22,17,15,26,51,19,12,35,40}; 

        vector< int > vec( ia, ia+12 );

        sort( &ia[0], &ia[12] );

        bool found_it = binary_search( &ia[0], &ia[12], 18 );

        sort( vec.begin(), vec.end(), greater<int>() );

        found_it = binary_search( vec.begin(), vec.end(), 26,

                                  greater<int>() );

        if(found_it)

                cout << "binary_search(): success!\n";

}

 

Output :

 

binary_search(): success!

 

lexicographical_compare : used to determine the lexicographical order of words.

 

#include <algorithm>

bool lexicographical_compare (InputIterator first1, InputIterator last1,

  InputIterator first2, InputIterator last2 [, BinaryFunction ] );

 

Example :

 

#include <algorithm>

#include <list>

#include <string>

#include <iostream>

using namespace std;

class size_compare {

public:

        bool operator()( const string &a, const string &b ) {

             return a.length() <= b.length();

        }

};

void main()

{

        string arr1[] = { "Piglet", "Pooh", "Tigger" };

        string arr2[] = { "Piglet", "Pooch", "Eeyore" };

        bool res;

        res = lexicographical_compare( arr1, arr1+3,

                                       arr2, arr2+3 );

        list< string > ilist1( arr1, arr1+3 );

        list< string > ilist2( arr2, arr2+3 );

        res = lexicographical_compare(

                 ilist1.begin(), ilist1.end(),

                 ilist2.begin(), ilist2.end(), size_compare() );

        if(res)

                cout << "ok: lexicographical_compare succeeded!\n";

}

 

Output :

 

ok: lexicographical_compare succeeded!

 

permutation : Get the previous or next permutation.

 

#include <algorithm>

bool next_permutation (BidirectionalIterator first,

     BidirectionalIterator last, [ Compare ] );

bool prev_permutation (BidirectionalIterator first,

     BidirectionalIterator last, [ Compare ] );

 

Example :

 

#include <algorithm>

#include <vector>

#include <iostream>

using namespace std;

bool nameCompare (char * a, char * b) { return strcmp(a, b) <= 0; }

void main ()

{

  ostream_iterator< char > out_stream( cout, " " );

  int start [] = { 1, 2, 3};

  do

     copy (start, start + 3, ostream_iterator<int,char> (cout, " ")), cout << endl;

  while (next_permutation(start, start + 3));

  char * words [] = {"Alpha", "Beta", "Gamma"};

  do

     copy (words, words + 3, ostream_iterator<char *,char> (cout, " ")), cout << endl;

  while (next_permutation(words, words + 3, nameCompare));

  vector<char> word(4);

  word[0] = 'b'; word[1] = 'e'; word[2] = 'l'; word[3] = 'a';

  do

        copy( word.begin(), word.end(), out_stream ), cout << endl;

  while (prev_permutation (word.begin(),word.end()));

  cout << endl;

}

 

Output :

 

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

3 2 1

Alpha Beta Gamma

Alpha Gamma Beta

Beta Alpha Gamma

Beta Gamma Alpha

Gamma Alpha Beta

Gamma Beta Alpha

b e l a

b e a l

b a l e

b a e l

a l e b

a l b e

a e l b

a e b l

a b l e

a b e l

 

sort : Sort the elements.

 

void sort (RandomAccessIterator first,

     RandomAccessIterator last [, Compare ] );

void stable_sort (RandomAccessIterator first,

     RandomAccessIterator last [, Compare ] );

 

The sort() algorithm is slightly faster, but it does not guarantee that equal elements in the original sequence retain their relative orderings in the final result. If order is important, the stable_sort() version should be used.

 

Example :

 

#include <algorithm>

#include <vector>

#include<queue>

#include <iostream>

using namespace std;

int randomInteger (unsigned int n) { return rand() % n; }   

int randomValue () { return randomInteger(100); }

void main ()

{

  vector<int> aVec(15);

  deque<int> aDec(15);

  ostream_iterator< int > out_stream( cout, " " );

  generate (aVec.begin(), aVec.end(), randomValue);

  generate (aDec.begin(), aDec.end(), randomValue);

  sort (aVec.begin(), aVec.end());

  copy( aVec.begin(), aVec.end(), out_stream ); cout << "\n";

  sort (aDec.begin(), aDec.end(), greater<int>() );

  copy( aDec.begin(), aDec.end(), out_stream ); cout << "\n";  

  sort (aVec.rbegin(), aVec.rend());

  copy( aVec.begin(), aVec.end(), out_stream ); cout << "\n";

}

Output :

 

0 5 24 27 34 41 45 58 61 62 64 67 69 78 81

95 95 92 91 91 82 53 42 36 27 21 18 16 4 2

81 78 69 67 64 62 61 58 45 41 34 27 24 5 0