Posts

Showing posts from February, 2017

C++ STL : Insertion Sort (Sorting)

#include<iostream>
#include<vector>
#include<algorithm>


void insertionSort(std::vector<int>&a){
typedef std::vector<int>::iterator it;
for(auto i=1;i<a.size();i++){
it end_sorted =a.begin()+i;
    it insertion_point=std::upper_bound(a.begin(),end_sorted,a[i]);
    std::rotate(insertion_point,end_sorted,end_sorted+1);
}
}
void print(std::vector<int>a){
for(int i=0;i<a.size();i++){
std::cout<<a[i]<<" ";
}

C++ STL :Recursive Merge Sort (Sorting)

#include<iostream>
#include<vector>
#include<algorithm>

template<typename T>

void merge(std::vector<T>& left_vector,std::vector<T>& right_vector,std::vector<T>& merged_vector){
auto left=left_vector.begin();
auto right=right_vector.begin();
auto merged=merged_vector.begin();
auto left_end=left_vector.end();
auto right_end=right_vector.end();

while(left!=left_end&&right!=right_end){
if(*right<*left){
*merged=*right;
++right;
}
else{
*merged=*left;
++left;
}
++merged;
}
std::copy(left,left_end,merged);
std::copy(right,right_end,merged);
}

template<typename T>
void merge_sort(std::vector<T>& A){

C++ Program: Counting letters,words,etc in input

#include <iostream>
#include <fstream>

int main()
{
std::string word;
std::getline(std::cin,word);
std::ofstream outf;
outf.open("lab3.txt");
outf<<word<<std::endl;
outf.close();
    std::ifstream lab3;

    lab3.open("lab3.txt");
    int countletters=0,countnum=0,countpunc=0,countspace=0,words=0,line=0;
    char character,prevchar = 0;
    if(!lab3)
    {
        std::cout << "Could not open file \n";
        return 1;
    }
    while(lab3.get(character) && !lab3.eof())
    {
        if(isalpha(character))
        {
            countletters++;
        }

C++ STL : Reading Elements from Vector until EOL

/* In this program we are entering elements in two vector. We can enter elements if we are pressing "space key".
 If we press "Enter" then we will start to enter in new vector
*/
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <iterator>
using namespace std;
std::vector<int> read_vector()
{
    std::string line = "";
    std::getline(std::cin, line);
    std::istringstream iss(line);

    std::vector<int> v;

    int x = 0;
    while (iss >> x)
        v.push_back(x);

    return v;
}

int main()
{