Tutorials Point, posted on 17th Jul, 2018

C++ String Library empty

#include <iostream>
#include <string>

int main () {
   char c;
   std::string str;
   std::cout << "Please type some lines of text. Enter a start (*) to finish:\n";
   do {
      c = std::cin.get();
      str += c;
      if (c=='\n') {
         std::cout << str;
         str.clear();
      }
   } while (c!='*');
   return 0;
}

Tutorials Point, posted on 17th Jul, 2018

C++ String Library clear

#include <iostream>
#include <string>

int main () {
   char c;
   std::string str;
   std::cout << "Please type some lines of text. Enter a start (*) to finish:\n";
   do {
      c = std::cin.get();
      str += c;
      if (c=='\n') {
         std::cout << str;
         str.clear();
      }
   } while (c!='*');
   return 0;
}

Tutorials Point, posted on 17th Jul, 2018

C++ Regex Library regex_token_iterator

#include <iostream>
#include <algorithm>
#include <iterator>
#include <regex>
 
int main() {
   std::string text = "Tutorialspoint india pvt ltd.";

   std::regex ws_re("\\s+"); 
   std::copy( std::sregex_token_iterator(text.begin(), text.end(), ws_re, -1),
      std::sregex_token_iterator(),
      std::ostream_iterator<std::string>(std::cout, "\n"));

   std::string html = "<p><a href=\"http://tutorialspoint.com\">google</a> "
      "< a HREF =\"http://indiabbc.com\">cppreference</a>\n</p>";
   std::regex url_re("<\\s*A\\s+[^>]*href\\s*=\\s*\"([^\"]*)\"", std::regex::icase);
   std::copy( std::sregex_token_iterator(html.begin(), html.end(), url_re, 1),
      std::sregex_token_iterator(),
      std::ostream_iterator<std::string>(std::cout, "\n"));
}

Tutorials Point, posted on 17th Jul, 2018

C++ Regex Library regex_iterator

#include <regex>
#include <iterator>
#include <iostream>
#include <string>

int main() {
   const std::string s = "Tutorialspoint.com india pvt ltd.";
 
   std::regex words_regex("[^\\s]+");
   auto words_begin = 
      std::sregex_iterator(s.begin(), s.end(), words_regex);
   auto words_end = std::sregex_iterator();
 
   std::cout << "Found " 
      << std::distance(words_begin, words_end) 
      << " words:\n";
 
   for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
      std::smatch match = *i;
      std::string match_str = match.str();
      std::cout << match_str << '\n';
   }
}

Tutorials Point, posted on 17th Jul, 2018

C++ Regex Library regex_replace

#include <iostream>
#include <string>
#include <regex>
#include <iterator>

int main () {
   std::string s ("there is a subsequence in the string\n");
   std::regex e ("\\b(sub)([^ ]*)");

   std::cout << std::regex_replace (s,e,"sub-$2");
  
   std::string result;
   std::regex_replace (std::back_inserter(result), s.begin(), s.end(), e, "$2");
   std::cout << result;

   std::cout << std::regex_replace (s,e,"$1 and $2",std::regex_constants::format_no_copy);
   std::cout << std::endl;

   return 0;
}

Tutorials Point, posted on 17th Jul, 2018

C++ Regex Library regex_search

#include <iostream>
#include <string>
#include <regex>

int main () {
   std::string s ("this subject has a submarine as a subsequence");
   std::smatch m;
   std::regex e ("\\b(sub)([^ ]*)");

   std::cout << "Target sequence: " << s << std::endl;
   std::cout << "Regular expression: /\\b(sub)([^ ]*)/" << std::endl;
   std::cout << "The following matches and submatches were found:" << std::endl;

   while (std::regex_search (s,m,e)) {
      for (auto x:m) std::cout << x << " ";
      std::cout << std::endl;
      s = m.suffix().str();
   }

   return 0;
}

Tutorials Point, posted on 17th Jul, 2018

C++ Locale Library constructor

#include <iostream>
#include <locale>

int main (void) {
   std::locale foo;
   foo.global(std::locale(""));
   std::locale bar;

   std::cout << "bar and foo both are ";
   std::cout << (foo==bar?"the same":"different");
   std::cout << ".\n";

   return 0;
}

Tutorials Point, posted on 13th Jul, 2018

C++ Exception Library bad_array_new_length

#include <iostream>
#include <new>
#include <climits>
 
int main() {
   int negative = -1;
   int small = 1;
   int large = INT_MAX;
   try {
      new int[negative];           
      new int[small]{1,2,3,4};       
      new int[large][50000000];     
   } catch(const std::bad_array_new_length &e) {
      std::cout << e.what() << '\n';
   }
}

Tutorials Point, posted on 12th Jul, 2018

C++ basic_ios Library gcount

#include <iostream>     

int main () {
   char str[20];

   std::cout << "Please, enter a word: ";
   std::cin.getline(str,20);
   std::cout << std::cin.gcount() << " characters read: " << str << '\n';

   return 0;
}

Tutorials Point, posted on 12th Jul, 2018

C++ basic_istream Library operator>>

#include <iostream>     

int main () {
   int n;

   std::cout << "Enter a number: ";
   std::cin >> n;
   std::cout << "You have entered: " << n << '\n';

   std::cout << "Enter a hexadecimal number: ";
   std::cin >> std::hex >> n;         
   std::cout << "Its decimal equivalent is: " << n << '\n';

   return 0;
}

1 2 3 4 5 6 7 ... 94 Next
Advertisements
Loading...