https://pastein.ru/t/rd

  скопируйте уникальную ссылку для отправки

Загрузка данных


// C++ program to demonstrate working of fil_n()
#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<int> vect(8);
    /*
                                           Void fill_n()
    void fill_n(iterator begin, int n, type value);
    begin: The function will start filling values from the position pointed by the iterator begin.
    n: This parameter denotes the number of positions to be filled starting from the position pointed by first parameter begin.
    value: This parameter denotes the value to be filled by the function in the containe
    calling fill to initialize first four values
    */
    fill_n(vect.begin(), 4, 7);

    for (int i = 0; i < vect.size(); i++)
        cout << ' ' << vect[i];
    cout << '\n';

    // calling fill to initialize 3 elements from
    // "begin()+3" with value 4
    fill_n(vect.begin() + 3, 3, 4);

    for (int i = 0; i < vect.size(); i++)
        cout << ' ' << vect[i];
    cout << '\n';
}
//                                      end

//                                  STATIC CLASS

/*
    static : This storage class is used to declare static variables which are popularly used while writing programs in C++ language.Static variables have a property of preserving their value even after they are out of their scope !Hence, static variables preserve the value of their last use in their scope.So we can say that they are initialized only once and exist until the termination of the program.Thus, no new memory is allocated because they are not re - declared.Their scope is local to the function to which they were defined.Global static variables can be accessed anywhere in the program.By default, they are assigned the value 0 by the compiler.
 */
// Function containing static variables
// memory is retained during execution
#include <iostream>
using namespace std;
int staticFun()
{
    cout << "For static variables: ";
    static int count = 0;
    count++;
    return count;
}

// Function containing non-static variables
// memory is destroyed
int nonStaticFun()
{
    cout << "For Non-Static variables: ";

    int count = 0;
    count++;
    return count;
}

int main()
{

    // Calling the static parts
    cout << staticFun() << "\n";
    cout << staticFun() << "\n";
    ;

    // Calling the non-static parts

    cout << nonStaticFun() << "\n";
    ;
    cout << nonStaticFun() << "\n";
    ;
    return 0;
}
/* Output:
    For static variables: 1
    For static variables: 2
    For Non-Static variables: 1
    For Non-Static variables: 1
    */
//                                               end

//                          Iomanip setbase() function in C++ with Examples

/*
The setbase() method of iomaip library in C++ is used to set the ios library basefield flag based on the argument specified as the parameter to this method.

Syntax:

setbase (int base)

Parameters: This method accepts base as a parameter which is the integer argument corresponding to which the base is to be set. 10 stands for dec, 16 stands for hex, 8 stands for oct, and any other value stands for 0 (reset).
*/

// C++ code to demonstrate
// the working of setbase() function

#include <iomanip>
#include <ios>
#include <iostream>

using namespace std;

int main()
{
    // Initializing the integer
    int num = 50;

    cout << "Before set: \n"
         << num << endl;

    // Using setbase()
    cout << "Setting base to hex"
         << " using setbase: \n"
         << setbase(16)
         << num << endl;

    return 0;
}
/*
    Output:
    Before set: 
    50
    Setting base to hex using setbase: 
    32
*/
//                                             end

/*                                  Manipulators with Arguments :
 Some of the manipulators are used with the argument like setw(20), setfill(‘*’) and many more.These all are defined in the header file.If we want to use these manipulators then we must include this header file in our program.

For Example,  you can use following manipulators to set minimum width and fill the empty space with any character you want : std::cout << std::setw(6) << std::setfill(’*’);

    Some important manipulators in <iomanip> are:
        setw (val): It is used to sets the field width in output operations.
        setfill (c): It is used to fill the character ‘c’ on output stream.
        setprecision (val): It sets val as the new value for the precision of floating-point values.
        setbase(val): It is used to set the numeric base value for numeric values.
        setiosflags(flag): It is used to sets the format flags specified by parameter mask.
        resetiosflags(m): It is used to resets the format flags specified by parameter mask.
    Some important manipulators in <ios> are:
        showpos: It forces to show a positive sign on positive numbers.
        noshowpos: It forces not to write a positive sign on positive numbers.
        showbase: It indicates numeric base of numeric values.
        uppercase: It forces uppercase letters for numeric values.
        nouppercase: It forces lowercase letters for numeric values.
        fixed: It uses decimal notation for ?oating-point values.
        scientific: It use scientific floating-point notation.
        hex: Read and write hexadecimal values for integers and it works same as the setbase(16).
        dec: Read and write decimal values for integers i.e. setbase(10).
        oct: Read and write octal values for integers i.e. setbase(10).
        left: It adjust output to the left.
        right: It adjust output to the right.
*/
#include <iomanip>
#include <iostream>
using namespace std;

int main()
{
    double A = 100;
    double B = 2001.5251;
    double C = 201455.2646;

    // We can use setbase(16) here instead of hex

    // formatting
    cout << hex << left << showbase << nouppercase;

    // actual printed part
    cout << (long long)A << endl;

    // We can use dec here instead of setbase(10)

    // formatting
    cout << setbase(10) << right << setw(15)
         << setfill('_') << showpos
         << fixed << setprecision(2);

    // actual printed part
    cout << B << endl;

    // formatting
    cout << scientific << uppercase
         << noshowpos << setprecision(9);

    // actual printed part
    cout << C << endl;
}
/* Output: 
0x64
_______+2001.53
2.014552646E+05
*/
//                                              end
//                                      remove and remove_if
/*
ForwardIterator remove  (ForwardIterator first,
ForwardIterator last, const T& val)

first,last :
  The range used is [first,last), which contains all the
elements between first and last, including the element
pointed by first but not the element pointed by last.

val :
Value to be removed.

Return value :
An iterator to the element that follows the last element not removed.
The range between first and this iterator includes all the elements
in the sequence that do not compare equal to val.
*/
/*короче создаёшь итератор для вектора и в него функция вернёт указатель на последний элемент после удаления,первый два параметра это указатели на какой диапазон надо применить функцию , третий значение которое надо удалить или функция ( remove_if ) которая вернёт true если надо удалить элемент.*/
#include <algorithm>
#include <iostream>
// Function to check whether
// the element is odd or not.
bool IsOdd(int i)
{
    return ((i % 2) == 1);
}

// Driver code
int main()
{
    std ::vector<int> vec1{10, 20, 30, 30, 20, 10, 10, 20};
    std ::vector<int> vec2{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    // Print original vector
    std ::cout << "Original vector : ";
    for (int i = 0; i < vec1.size(); i++)
        std ::cout << " " << vec1[i];
    std ::cout << "\n";

    // Iterator that store the position of last element
    std ::vector<int>::iterator pend;

    // std ::remove function call
    pend = std ::remove(vec1.begin(), vec1.end(), 20);

    // Print the vector
    std ::cout << "After remove : ";
    for (std ::vector<int>::iterator p = vec1.begin(); p != pend; ++p)
        std ::cout << ' ' << *p;
    std ::cout << '\n';

    // Print original vector
    std ::cout << "\nOriginal vector : ";
    for (int i = 0; i < vec2.size(); i++)
        std ::cout << " " << vec2[i];
    std ::cout << "\n";

    // std ::remove_if function call
    pend = std ::remove_if(vec2.begin(), vec2.end(), IsOdd);

    // Print the vector
    std ::cout << "After remove_if : ";
    for (std ::vector<int>::iterator q = vec2.begin(); q != pend; ++q)
        std ::cout << ' ' << *q;
    std ::cout << '\n';

    return 0;
}
/*
Output:
Original vector :  10 20 30 30 20 10 10 20
After remove :  10 30 30 10 10

Original vector :  1 2 3 4 5 6 7 8 9 10
After remove_if :  2 4 6 8 10
*/