Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) StdStrchrExample1

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppStdStrchrExample1/CppStdStrchrExample1.pro

 

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

 

 

 

 

 

./CppStdStrchrExample1/main.cpp

 

#include <algorithm>
#include <cassert>
#include <cstring>
#include <string>

int main()
{
  const std::string s = "ABXDEF";

  //C style way to find a character in a string
  const char * const c = std::strchr(&s[0],'X');
  assert(*(c-1)=='B');
  assert(*(c+1)=='D');

  //Similar C++ style way to find a character in a string
  const std::string::const_iterator i
    = std::find(s.begin(),s.end(),'X');
  assert(*(i-1)=='B');
  assert(*(i+1)=='D');
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml