Showing posts with label STL. Show all posts
Showing posts with label STL. Show all posts

5/25/2018

print each character in string and some useful functions

This is example source code for how to print and access each character in string.
And using this function, I made some useful functions.
Thank you.


...
void printAndCountAllCh(string str)
{
    int count = 0;
    for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
        std::cout << count << ": " << *it << endl;
        count++;
    }
}

string changeAtoB(string str, char A, char B)
{
    int count = 0;
    for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
        if (string(1, *it) == string(1, A))
        {
            *it = B;
        }
    }

    return str;
}

string deleteA(string str, char A)
{
    string rstr;
    int count = 0;
    for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
        if (string(1, *it) != string(1, A))
        {
            rstr.push_back(*it);
        }
    }

    return rstr;
}


int countNotCH(string str, char CH)
{
    int count = 0;
    for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
        if (string(1, *it) != string(1, CH))
            count++;
    }
    return count;
}

int main()
{

    string str = "A_B CD_EF GH_I";
    int count;

    printAndCountAllCh(str); //print each char and count
    std::cout << endl;

    string abc = changeAtoB(str, ' ', '_'); //A_B CD_EF GH_I -> A_B_CD_EF_GH_I
    std::cout << abc << endl << endl;

    string bbb = deleteA(str, ' '); //A_B CD_EF GH_I -> A_BCD_EFGH_I
    std::cout << bbb << endl << endl;

    count = countNotCH(str, '_'); //A_B CD_EF GH_I -> 11
    std::cout << count << endl << endl;

    return 0;
}
...




3/16/2018

simple tip: int to string in stl

memo for me.. ^^

for(int i=0; i<10; ++i)
     std::string index1("string_" + std::to_string(i) + ".jpg");



9/27/2017

Tip, Add 'Vector (b)' to end of 'Vector (a)'

If you have 2 Vector and you want to add Vector b to end of Vector a, refer to below code. (very simple!)

vector< int > a;
a.push_back(1);
a.push_back(2);
a.push_back(3);

vector< int > b;
b.push_back(4);
b.push_back(5);
b.push_back(6);

a.insert(a.end(), b.begin(), b.end());

int count = 0;
for (auto it : a)
{
printf("a[%d] = %d \n", count++, it);
}



6/14/2017

stl vector test for pop back and pop front


STL Vector test for pop_back, pop_back_n and pop front.

I wanted to erase first element.
but there is no inner function.
pop_back, pop_back_n is that erase last element in vectors.
The last sample is the method to pop front.

refer to this sample code.

< gist >



< /gist >


7/29/2014

no duplication random, (stl random_shuffle example source code)

sometimes, we need no duplication random sequence data.
simple method is using shuffle random in stl.

refer to this source code.

#include < stdio.h>
#include < vector>
#include < algorithm>
#include < cstdlib>
#include < ctime>
#include < iostream>
using namespace std;


void main()
{
 //set size and initialize
 vector< int > A(10);
 for(int i=0; i< A.size(); ++i)
  A[i] = i;

 //confirm
 printf("----origin data \n");
 for(int i=0; i< A.size(); ++i)
  printf("[%d] - %d \n", i, A[i] );
 printf("----\n");

 //random 
 srand( unsigned (time(0) ) );
 random_shuffle( A.begin(), A.end() );

 //confirm
 printf("---- After shuffle \n");
 for(int i=0; i< A.size(); ++i)
  printf("[%d] - %d \n", i, A[i] );
 printf("----\n");
}


..

7/08/2014

STL vector sort, lambda version


//vector
vector< pair > vote;

//data input
~~
~~

//sort
 sort(vote.begin(), vote.end(), [](const pair& A, const pair& B){ return A.second > B.second; } );

10/21/2011

STL vector(with 2 more elements) sorting code / STLμ—μ„œ 2개 μ΄μƒμ˜ μ—˜λ¦¬λ¨ΌνŠΈλ₯Ό 가지고 μžˆλŠ” vector μ •λ ¬ code

The source is simple but I always don't remember the code well.
If you similar to me, refer to my code easily.


#include <iostream>
#include <cmath>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;

class element{
public:
    element(float a, int b){
        value = a;
        index = b;
    }
    float value;
    int index;
};

bool compare(const element &a, const element &b )
{
    return a.value < b.value; //μ˜€λ¦„μ°¨μˆœ(ascending order)
    //return a.value > b.value; //λ‚΄λ¦Όμ°¨μˆœ(descending order)
}

void main()
{
    srand( (unsigned int)time(NULL));

    vector< element > A;

    // κ°’ λ„£κΈ° (input value)/////////////////////////////////////////////
    for(int i=0; i<20000; ++i){
        A.push_back( element(rand()%10, i) );
    }


    //κ°’ 좜λ ₯  (data print)///////////////////////////////////////////////
    printf("μ •λ ¬ μ „(Before ordering) \n");
    for(i=0; i<20000; ++i){
        printf("A[%d] -> value %f :: index -> %d\n", i, A[i].value, A[i].index );
    }


    //μ •λ ¬ (sorting)
    sort(A.begin(),A.end(),compare);


    //κ°’ 좜λ ₯ (Value print)
    printf("μ •λ ¬ ν›„(After ordering)\n");
    for(i=0; i<20000; ++i){
        printf("A[%d] -> value %f :: index -> %d\n", i, A[i].value, A[i].index );
    }
      
}