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;
}