Aug 27, 2018

C++ Basics and Python

Python

dictionary get() Method

The method get() returns a value for the given key. If key is not available then returns default value None.

dict.get(key, default = None)
  • key − This is the Key to be searched in the dictionary.

  • default − This is the Value to be returned in case key does not exist.

dict = {'Name': 'Zabra', 'Age': 7}
print "Value : %s" %  dict.get('Age')
print "Value : %s" %  dict.get('Education', "Never")
Output:

Value : 7
Value : Never

C++

abs()

Returns the absolute value of parameter n .

/* abs example */
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* abs */

int main ()
{
  int n,m;
  n=abs(23);
  m=abs(-11);
  printf ("n=%d\n",n);
  printf ("m=%d\n",m);
  return 0;
}
Output

n=23
m=11

std::tolower(charT c, const locale& loc)

Converts parameter c to its lowercase equivalent if c is an uppercase letter and has a lowercase equivalent, as determined by the ctype facet of locale loc. If no such conversion is possible, the value returned is c unchanged.

// tolower example (C++)
#include <iostream>       // std::cout
#include <string>         // std::string
#include <locale>         // std::locale, std::tolower

int main ()
{
  std::locale loc;
  std::string str="Test String.\n";
  for (std::string::size_type i=0; i<str.length(); ++i)
    std::cout << std::tolower(str[i],loc);
  return 0;
}

std::string::erase()

sequence (1)	string& erase (size_t pos = 0, size_t len = npos);
character (2)	iterator erase (const_iterator p);
range (3)	    iterator erase (const_iterator first, const_iterator last);

Erases part of the string, reducing its length

  1. sequence: Erases the portion of the string value that begins at the character position pos and spans len characters (or until the end of the string, if either the content is too short or if len is string::npos. Notice that the default argument erases all characters in the string (like member function clear).

  2. character: Erases the character pointed by p.

  3. range: Erases the sequence of characters in the range [first,last).

string remove_rubbish(string s) {
    auto is_rubbish = [](char c) { 
        return ispunct(c) || isspace(c); 
    };
    
    // remove spaces and puctuations
    s.erase(std::remove_if(s.begin(), 
                           s.end(), 
                           is_rubbish), 
            s.end());
    return s;
}

std::string::substr()

string substr (size_t pos = 0, size_t len = npos) const;

Returns a newly constructed string object with its value initialized to a copy of a substring of this object. The substring is the portion of the object that starts at character position pos and spans len characters (or until the end of the string, whichever comes first).

  • pos

    • Position of the first character to be copied as a substring. If this is equal to the string length, the function returns an empty string. If this is greater than the string length, it throws out_of_range. Note: The first character is denoted by a value of 0 (not 1).

  • len

    • Number of characters to include in the substring (if the string is shorter, as many characters as possible are used). A value of string::npos indicates all characters until the end of the string.

#include <iostream>
#include <string>

int main ()
{
  std::string str="We think in generalities, but we live in details.";
                                           // (quoting Alfred N. Whitehead)

  std::string str2 = str.substr(3,5);     // "think"

  std::size_t pos = str.find("live");      // position of "live" in str

  std::string str3 = str.substr(pos);     // get from "live" to the end

  std::cout << str2 << ' ' << str3 << '\n';

  return 0;
}

Last updated