Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Creating a segment tree in C++ and update a particular value

//segment tree making and update a particular value
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<cstring>
using namespace std;

void makest(int arr[],int st[], int ss, int se, int si)
{
    if( ss==se)
    {
        st[si]=arr[se];
        return ;
    }
    int mid=(se+ss)/2;
    makest(arr, st, ss, mid, 2*si+1);
    makest( arr, st, mid+1, se, 2*si+2);
    st[si]=st[2*si+1]+ st[2*si+2];
}
void updatest(int st[], int ss, int se, int si,int index,int diff)
{
    if( index <ss || index >se)
    {
        return;
    }
        st[si]=st[si]+diff;
        if( se!=ss)
        {
        int mid=(ss+se)/2;
        updatest(st, ss, mid, 2*si+1, index,diff);
        updatest(st, mid+1,se,  2*si+2, index ,diff);
        }
}
int main()
{
    int arr[]={1,2,3,4,5,6};
    int height=(int)(ceil(log2(6)));
    int size=(int)2*((int)pow( 2, height)-1);
    int st[size];
    memset( st, 0, sizeof(st));
    makest(arr, st, 0, 5, 0);
    for ( int i=0;i< size;i ++)
    {
        cout<< st[i]<<endl;
    }
    int index=3;
    int diff=2;
    updatest( st, 0, size, 0, index, diff);
    cout<< "after upadation in my segment tree";
    for ( int i=0;i< size;i ++)
    {
        cout<< st[i]<<endl;
    }
    return 0;
}

Printing Example in C++

#include <iostream>

using namespace std;

int main()
{
   cout << "Hello World" << endl; 
   for(int i=0;i<24;i++){
       cout<<"sysvar::VTS::M1_Ch"<<2*i-1<<".SetOutputMode(eVTS2848OutputModePushPull);"<<endl;

   }
   
   return 0;
}

design pattern_1

#include <iostream>

using namespace std;

int main()
{
   cout << "Builders" << endl; 
   
   return 0;
}

Convert Number to Text using C++11

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

using namespace std;

std::string number_to_text(int number)
{
    std::vector<std::string> ones{"", "one", "two", "three", "four", "five","six", "seven", "eight", "nine"};
    
    std::vector<std::string> tens{"", "ten", "twenty", "thirty",
        "fourty", "fifty", "sixty", "seventy", "eight", "ninety"};
        
    std::vector<std::string> hundreds{"", "one hundred", "two hundred",
        "three hundred", "four hundred", "five hundred", "six hundred",
        "seven hundred", "eight hundred", "nine hundred"};
        
    std::vector<std::string> thousands{"", "one thousand", "two thousand",
        "three thousand", "four thousand", "five thousand",
        "six thousand", "seven thousand", "eight thousand",
        "nine thousand"};
        
    std::vector<std::vector<std::string>> map;
        
    std::string text;
        
    map.push_back(ones);
    map.push_back(tens);
    map.push_back(hundreds);
    map.push_back(thousands);
    
    int step = 0;
    
    while (number > 0)
    {
        int digit = number % 10;
        number = number / 10;
        
        text.insert(0, " ");
        text.insert(0, map.at(step).at(digit));
        
        step = step + 1;
    }
    
    text.pop_back();
    return text;
}

int main()
{
   std::cout << number_to_text(1972) << std::endl;
   std::cout << number_to_text(1970) << std::endl;
   std::cout << number_to_text(1900) << std::endl;
   std::cout << number_to_text(1000) << std::endl;
   
   return 0;
}

Compile and Execute C++11 Online

#include <iostream>

using namespace std;

int main()
{
   cout << "Hello World" << endl; 
   
   return 0;
}

#include <stdio.h> int main() { /* my first program in C */ printf("hellow, World! \n"); return 0; }

#include <stdio.h>

int main()
{
    /* my first program in C */
    printf("hellow, World! \n");
    

    return 0;

}

callback server example

#include <functional>
#include <iostream>

struct Server {
   void print_add(int i) const { std::cout << i << '\n'; }
   int num_;
   
   std::function<void(Server*)> callback_function_t[10];
   int count = 0;

   void addSite(std::function<void(Server*)> func)
   {
       callback_function_t[count++]=func;
   }
  
   void config()
   { 
       print_add(0);
       for(int i=0;i<10;i++)
       if(callback_function_t[i])
       callback_function_t[i](this); 
       
   };
};
 

struct Site 
{
   void call(Server *server)
   {
       server->print_add(1);
    };
};

struct Site2 
{
   void call(Server *server)
   {
       server->print_add(2);
    };
};


int main() {
    
    Site site;
    Site2 site2;
    
    Server server;
    using std::placeholders::_1;
    server.addSite(std::bind(&Site::call,&site,_1));
    server.addSite(std::bind(&Site2::call,&site2,_1));
    server.config();

}

Compile and Execute C++11 Online

#include <iostream>
#include <cstring>
// using namespace std;

class String
{
    public:
        String()
        {
            
        }
        String(char* s)
        {
            length=sizeof(s)/sizeof(*s);
            _s=new char[length];
            strcpy(_s,s);
        }
        std::ostream& operator<<(std::ostream& os)
        {
            for(int i=0;i<len;++i)
                os<<*(val._s+i);
            return os;
        }
    private:
        char* _s;
        int length;
};

int main()
{
    char str1[]="Hello World";
//   std::cout << sizeof(s)/sizeof(*s) -1 << std::endl; //-1 for '\0'
   String str(str1);
//   std::cout<<str<<std::endl;
    return 0;
}

string_class

#include <iostream>

using namespace std;

class String
{
    public:
        String(char* str)
        {
            str_obj=str;
        }
        String operator + (const String& str1)
        {
            String str_return;
            return strcat(this,str1);
        }
    private:
        std::string str_obj;
};

int main()
{
    String str1("char");
    String str2("actor");
    String str3=str1+str2;
   return 0;
}

filelock

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>

int main()
{
	int file_desc;
	int save_errno;
	
	file_desc = open("/tmp/LCK.test", O_RDWR|O_CREAT|O_EXCL, 0444);
	if(file_desc == -1)
	{
		save_errno = errno;
		printf("Open failed with error %d\n", (save_errno));
		printf("Open failed with error %s\n", strerror(errno));
	}
	else
	{
		printf("Open succeeded\n");
	}
	exit(EXIT_SUCCESS);
}

1 2 3 4 5 6 7 ... 151 Next
Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.