FrontPage FindPage TitleIndex RecentChanges UserPreferences E D R S I H C
 
Cplusplus Tips
HelpContentsTitleIndexWikiSandBox#previewMoniWikiPluginsairfare-priceline-16 › CplusplusTips

istream_iterator and pair

http://discuss.joelonsoftware.com/default.asp?joel.3.191244.5

Currently we should define in a std namespace to go around.

std::auto_ptr

Templated class defiend in <memory>. Extremely useful when you need to use a heap object like a stack object. Usually ctor, reset are used.

The two examples below are equivalent.

#include <memory>

int main(void)
{
  std::auto_ptr<int> p(new int(1));
// ...
  if (/* some condition */)
  {
    p.reset(new int(1));
  }
// ...
  return 0;
}



#include <memory>

int main(void)
{
  int* p = new int(1);
// ...
  if (/* some condition */)
  {
    delete p;
    p = new int (1);
  }
// ...
  delete p;
  return 0;
}

string replace function

#include <iostream>
#include <string>
using namespace std;

string replace_str(const string& s, const string& from, const string& to)
{
        string str;
        int i=0,j=0;
        while ( ( j = s.find(from,i) ) != -1 )
        {
//              cerr <<i <<','<<j <<endl;
                str += s.substr(i,j) + to;
                i = j + from.length();
        }
        str += s.substr(i);
        return str;
}

int main(void)
{
        c();
        string aaa ="abcdefgcdefga";
        cout << aaa << endl;
        aaa = replace_str(aaa,"cde","@@@@");
        cout << aaa << endl;
}


CategoryCplusplus
last modified 2009-03-09 12:46:40
EditTextFindPageDeletePageLikePages