marți, 14 martie 2017

npos c++ example

c++ > <string> > npos 

// bad/missing length/position

An unsigned integral value initialized to –1 that indicates either "not found" or "all remaining characters" when a search function fails.

static const size_type npos = -1;  

Example

#include "stdafx.h"

#include <iostream>
#include <string>

int main()
{
       std::string input = "input string";

       if (input.find('b') == std::string::npos)
       {
              std::cout << "no 'b' found in 'input string'\n";
       }

       if (input.find('p') != std::string::npos)
       {
              std::cout << "'p' found in 'input string'\n";
       }

}

miercuri, 21 decembrie 2016

count_if c++ example

c++ > algorithm > count_if

count_if returns the number of elements in a range whose values satisfy a specified condition.

template<class InputIterator, class Predicate>
   typename iterator_traits<InputIterator>::difference_typecount_if(
      InputIterator _First, 
      InputIterator _Last,
      Predicate _Pred
   );

Example

#include "stdafx.h"
#include <iostream>
#include <algorithm> 
#include <vector> 

using namespace std;

bool IsEven(int n)
{
       return ((n % 2) == 0);
}

int main() {
       std::vector<int> numbers;
       for (int i = 1; i<10; i++)
              numbers.push_back(i);

       int evencount = count_if(numbers.begin(), numbers.end(), IsEven);
       std::cout << "numbers contains " << evencount << " even values.\n";

       return 0;

}

Result:


marți, 6 decembrie 2016

C++ Pointers Example

C++ > Pointers

Syntax:

type * name;


A variable which stores the address of another variable is called a pointer.

Pointers point to the variable whose address they store.

Example


#include "stdafx.h"

int main()
{
       int var = 25;
       int *adr = &var;
       // *adr = 25
       // adr   = 0x0042fc1c

       *adr = 30;
       // var = 30

    return 0;
}


joi, 17 martie 2016

C++ Map class emplace example

C++ > Map class > Emplace

Inserts an element constructed in place into a map.



Example 

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <map>
#include <string>
#include <iostream>
using std::endl;
using std::cout;
using std::cin;

int _tmain(int argc, _TCHAR* argv[])
{
       std::map <int, std::string> map;

       auto ret1 = map.emplace(1, "one");
       auto ret2 = map.emplace(2, "two");
       auto tree1 = *ret1.first;
       auto tree2 = *ret2.first;

       cout << "First element is (" << tree1.first << ", " << tree1.second << ")" << endl;
       cout << "Second element is (" << tree2.first << ", " << tree2.second << ")" << endl;

       int ret;
       cin >> ret;

       return ret;

}

miercuri, 16 martie 2016

#pragma once C# example

C++ > Pragma directives > #pragma once

Specifies that the file will be included only once by the compiler when compiling a source code file, so the build times is reduced.

Example:

HeaderClass.h


#pragma once
// Code placed here is included only once 
namespace Application{
class HeaderClass
{
public:
    HeaderClass();
};
} // namespace Application

joi, 14 mai 2015

Cout Cin C++ Example

C++ > iostream > cout, cin

Example

# include "stdafx.h"
# include <iostream>
using  namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
       int number;
       cout << "Please enter a number: ";
       cin >> number;
       cout << "The number you entered is " << number;
       cin >> number;
       return 0;

}

miercuri, 25 februarie 2015

C++ Preprocessor

C++ > Preprocessor 

Preprocessor performs preliminary operations on C++ files before they are passed to the compiler. 

  • insert files
  • compiler error messages
  • conditionally compile code 
The preprocessor is a text processor that manipulates the text of a source file as part of the first phase of translation. The compiler ordinarily invokes the preprocessor in its first pass, but the preprocessor can also be invoked separately to process text without compiling.