Tuesday, October 20, 2015

c++ vector

c++ vector

Vector is like array.
In array, you cannot change the length, but you can do that in vector.
It has index like array. syntax like "vector[3]= 3" is totally fine in vector

Format

std :: vector
     -> if you use "using namespace std", you do tno need to add "std ::"

constructor
     vector <type> name
     vector <type> name (length, val)
     vector <type> name (array , size of array)
         - type can be anything(string, int, char and other )

    .size()
        - return size of vector

    .insert( "interator", "val" )
        - add new element to vector. The element would be added at index. The elements in the vector shift one to right
        - you need iterator vector<int>::iterator 

    .push_back( "val" )
        - add new element to vector. The element will be stored at the end of vector. 

    .erase( "iterator" )
       - delete element of vector
       - if your vector is called input3, treat input3.begin() as index 0
                -> if you want to delete third element of vector, it would be input3.begin() +2
       - once the element is deleted, elements after deleted elements would shift one to left

    .clear()
       - delete all element of vector

------------------------------------------------------------------------------------------------------------------------

The following is practice code. If you have suggestion or correction, please feel free to leave comment.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char** argv) {

    int array[] = {1,2,3,4};
   
    //1
    //constructors of vector
    vector <int> input (3,100); //length = 3 val = 100
    vector <int> input2 (array, array + sizeof(array) / sizeof(int));// constructor with array
    vector <int> input3;// empty vector
   
    //2
    //print element of vector. it is like array
    for(int i=0; i< input.size(); i++){
        cout << input[i] << endl;
    }
    cout << "----------------------------" << endl;
   
    //3
    //print element of vector. it is like array
    for(int i=0; i< input2.size(); i++){
        cout << input2[i] << endl;
       
    }
    cout << "----------------------------" << endl;
   
    //3
    //insert elements to vector
    //if you use insert, the vector element is stored at the very first index. the rest
    //are shift to right by one
    //if you use push_back, the vector element is stored at the end of vector
    vector<int>::iterator it;
    it = input3.insert(it , 300);
    it = input3.insert(it , 400);
    it = input3.insert(it , 100);
    input3.push_back( 999 );
   
    for (int i=0; i<input3.size(); i++){
        cout << input3[i] << endl;
    }
   
    cout << "----------------------------" << endl;
    //4
    //delete 2 elements of input3 vector
    //index(int) wont work. you need to use either begin or iterator
    input3.erase (input3.begin()+1);
   
    for (int i=0; i<input3.size(); i++){
    cout << input3[i] << endl;
    }
   
    cout << "-----------------------------" << endl;
    //5
    //clear all contents of vector
    input.clear();
    if (input.size() == 0)
        cout << "there is nothing in input vector" << endl;
    return 0;
}



Monday, October 19, 2015

c++ i/o stream

c++ i/o stream

In order to use data in disk, monitor, keybord and other input/output devices, we need to use stream.
















We can think the stream as a bridge that connect your c++ program with oother input device.
For example, I am going to demonstrate your " cout " command.

cout << "hello world" ;

your c++ program runs " cout " --> stream is created --> your c++ programs grabs "hello world" --> "hello world" is sent to monitor via stream --> "hello world" is displayed on your monitor.

files and other input/output devices have same behaviour

I am going to show you how I can read and write text file using ifstream and ofstream.
ifstream = reading from a file
ofstream = writing to a file
---------------------------------------------------------------------------------------------------------------------------

 The following is practice code. If you have suggestion or correction, please feel free to leave comment.


#include <iostream>
#include <fstream>
using namespace std;

int main() {
    //writing sth to " test.txt " file
    ofstream f;
    f.open("test.txt");
    f << "hello world" << endl;
    f.close();

    //read " text2.txt " file line by line
    ifstream ff ("text2.txt");
   
    //check whether text2.txt is usable file
    if( ff.is_open())
    {
         string line;

         //getline returns true until there is sth to read in the file
         //reading line is stored in string variable
         while ( getline( ff, line ))
        {
                cout << line << endl;
        }
    }

    ff.close()


}


Sunday, October 18, 2015

c++ string

c++ string

String is like char * in C.
In my opinion, it is easier and safter to use string instead of  char *.
You do not have to worry about memory leak; in C, you need to make sure the length and delete.

Format

std :: string
     -> if you use "using namespace std", you do not need " std:: "

    .length()
        - return word length of string

    .at( "position" )
         - return a string at the postion

    .substring("start point" ,  "length")
        - return sub string of string

    .compare( "compare string")
        - compare two strings. If they are same, it returns 0
 
    + operator
         - add two strings

    .find( "wanted string" )
         - if "wanted string" is in the string, return the first position of the string
         - if there isn't "wanted string", it returns 18446744073709551615 (= string::npos )             

    .erase("start point", "length")
         - erase part of string from starting point until the length

    .replace("position", "length", "replacing string")
         - grab sub string starting at specified postion untill the lenth and repace with the "replacing
            string"
 -------------------------------------------------------------------------------------------------------------------------

The following is practice code. If you have suggestion or correction, please feel free to leave comment.

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char** argv) {

    string input = "hello world";
    string input2 = "hello world";
    string input3 = "hello world2";
   
    //1
    //getting length of string
    int length = input.length();
   
    //2
    //grab a component of string
    for(int i=0; i< length; i++){
        cout << input.at(i) << endl;
    }
    cout << endl;
   
    //3
    //substring start position of 1 upto lenth 5
    string subString = input.substr(1,5);
    cout << subString << endl << endl;

    //4
    // compare two strings are equal, if they are, return 0
    cout << "compare same: " << input.compare(input2) << endl;
    cout << "compare different: " << input.compare(input3) << endl;
    cout << endl;
   
    //5
    // add two string
    cout << "add two strings : " << input + input2 << endl;
   
    //6
    // find string
    cout << "where is lo : " << input.find("lo") << endl << endl;
    cout << "where is z : " << input.find("z") << endl << endl << endl;
  
    //7
    // delete part of string
    input2.erase(0,5);
    cout << "delete position 0 upto length 5 : " << input2 << endl;
   
    //8
    // replace string
    input2.replace(0, 0, "hi");
    cout << "repalce : " << input2 << endl;
   
     return 0;
}

Saturday, October 17, 2015

c++ set

c++ set

Set is very useful data structure in C++.
It is like an array but it has lots of useful tools.
Tools that I often use and introduce are..

Format
std :: set < type > setName
    -> if you use "using namesapace std", you do not need std ::
    -> type can be int, char, string and vice versa

    .insert( "item" )
        - insert value in a set

    .size(   )
        - check number of item in a set

    .find( "item")
    .find( "index")
       - find item by using element or index.
       - This function is really powerful because it find element in O(1).
             -> every element is in hash. when it search for item, what it needs to is looking the hash value
       - if find cannot find it returns .end ()

    .erase( "item" )
    .erase( "index" )
        - erase element by using element or index

If you want to grab a element from set,  use iterator
ex) 
  set < string > :: iterator checker
  checker = yourSet.find("item")
  cout << *checker << endl;
------------------------------------------------------------------------------------------------------------------------------

The following is practice code. If you have suggestion or correction, please feel free to leave comment

#include <iostream>
#include <set>
using namespace std;

int main(int argc, char** argv) {
   
    set <string> aSet;
    set <string>::iterator checker;
    set <string>::iterator element;
   
    string input1 = "one";
    string input2 = "two";
    string input3 = "three";
    string input4 = "four";
    string inputDummy = "one";
   
    aSet.insert( input1 );
    aSet.insert( input2 );
    aSet.insert( input3 );
    aSet.insert( input4 );
    aSet.insert( inputDummy );
   
    // 1
    //return three because input1 and inputDummy have same value
    cout << aSet.size() << endl;
   
    //2
    //return one because set has one
    checker = aSet.find("one");
    if( checker != aSet.end() )
        cout << "one" << endl;
   
    //3
    //return none because has one
    checker = aSet.find("five");
    if( checker == aSet.end() )
        cout << "five is not in the set" << endl << endl;
   
    //4
    //print every elements
    //print in alphabetical order
    for( element = aSet.begin(); element != aSet.end() ; element++ ){
        cout << *element << endl;
    }
   
    //5
    //erase element of set by value and index
    aSet.erase( "one" );
    aSet.erase( input2 );
    cout << endl;
   
    //6
    //print every elements
    for( element = aSet.begin(); element != aSet.end() ; element++ ){
        cout << *element << endl;
    }
   
   
    return 0;
}