Skip to main content

Useful C++ code snippets


Setting up  Precision to given decimal point 
“std:setprecision()”  method used along with  “std:fixed” provides precision of 
outputting floating point numbers correct to decimal numbers mentioned in the parameter 
of the setprecision method.
header file
#include <iomanip>
std:setprecision() defined no of digits in number. Without using std:fixed() method setprecision()  will   limit whole numbers digits to given precision.
In order to print precision of decimal point both setprecision() and fixed() method using before printing values,
code snippet :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// setprecision example

#include <iostream>     // std::cout, std::fixed
#include <iomanip>      // std::setprecision

int main(){

     double   doubeleValue =3.34535 ;

     std::cout <<  std::setprecision(4) ;
     std::cout <<  doubeleValue<< '\n';
      // Output : 3.345  -without fixed


      std::cout << std::fixed;
      std::cout <<  doubeleValue<< '\n';// Output : 3.3453

     std::cout <<  std::setprecision(2) <<std::fixed<< doubeleValue<< '\n';
      // Output : 3.34

    return 0;
}  



Getting input from Terminal / CommandLine


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// setprecision example

#include <iostream>
#include <string>

using namespace std;

int main(){

    int getInt;
    std::cout << "Get Int : " ;

    cin >> getInt  ;
    std::cout << "int " << getInt<<'\n';
    // using cin >> you can get any primative data type

    /*  to get whole line as string use string and getline method
    */
    string str;
    std::cout << "String :" ;
    std::getline(std::cin,str);
    std::cout << str << '\n';

    return 0;
}


Getting multiple numeric values in same Line of Terminal/CMD
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <sstream>
#include <iterator>
#include <string>
#include <vector>

int main() 
{
    std::string s;

    std::getline( std::cin, s );

    std::istringstream is( s );

    std::vector<int> v( ( std::istream_iterator<int>( is ) ), std::istream_iterator<int>() );

    for ( int x : v) std::cout << x << ' ';
    std::cout << std::endl;

    return 0;
}

Getting the power of value
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
/* pow example */
#include <stdio.h>      /* printf */
#include <math.h>       /* pow */

int main ()
{
  printf ("7 ^ 3 = %f\n", pow (7.0, 3.0) );
  printf ("4.73 ^ 12 = %f\n", pow (4.73, 12.0) );
  printf ("32.01 ^ 1.54 = %f\n", pow (32.01, 1.54) );
  return 0;
}

Reading a text file
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// ios::exceptions
#include <iostream>
#include <fstream>
using namespace std;

void do_something_with(char ch) {} // Process the character 

int main () {
  ifstream file;
  file.exceptions ( ifstream::badbit ); // No need to check failbit
  try {
    file.open ("test.txt");
    char ch;
    while (file.get(ch)) do_something_with(ch);
    // for line-oriented input use file.getline(s)
  }
  catch (const ifstream::failure& e) {
    cout << "Exception opening/reading file";
  }

  file.close();

  return 0;
}

Writing a text file
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// writing on a text file
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile ("example.txt");
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}

Calculating the run time of given code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// writing on a text file
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile ("example.txt");
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}



 

Comments

Popular posts from this blog

Simple Approch to Big Data

Big Data   “ Big Data is data sets that are so voluminous and complex that can’t be handle through tradition   statistical and Data Science approaches. “ All the time, Peoples and the rest of universe (electrons, protons,atoms,molecules,trees,animals,birds, rivers,sea,air, weather ,world,sky ,stars,planets,...whole the universe) generating massive amounts of  data which we didn’t gave notice  until now . But with the Internet ,WWW and recent emergence of Smart devices(Sensors ,mobiles ,Internet of  Things  IOT )capturing those massive flood of data and sinking both Academia and Enterprises  within   by urging to handle them efficiently. Big data is often characterized by 3Vs : the extreme V olume of data, the wide V ariety of data types the V elocity at which the data must be processed. Although big data doesn't equate to any specific volume of data, the term is often used to describe terabytes...

My IEEE Story

As a proud student member of IEEE. I would like to say IEEE is a great place to build your professional life and gain experiences.IEEE is a great place for those students who are curious to gather knowledge .not the only knowledge, programmes held by IEEE are based on fun and enjoyment too.  IEEE Student/Young professional / Women in engineering Congress 2017 can be mention as an example for such events. It was one of the best events I participated in my undergraduate life.lots of volunteers were participated in that. there I was able to build up a lot of friendships and relationships with the volunteers all around the country .we share those relations even after the Congress which help us to share our ideas and knowledge .that help me to solve problems which are impossible for me to solve alone. I got this golden chance only because of IEEE .i will always be thankful to IEEE for giving us such opportunities. Not only that, the fun,  glamour,  education,  ...

GSoC with ScholerX_2

 How  proceesed on GSoC once you  selected  organizations ?                                   Once you selected organizations which are suite your field of interest then you should look for most suitable for you. Since GSoC is very competitive, you have to dedicated for one or two particular organizations. So select the most suitable organization which have projects suitable for your interest area. Once you selected an organization very First thing to do is subscribing their mailing list. Then check their past projects and issues in their Github profile. Most of the organization maintain projects idealist in their Github repository.  Go through all of projects idealist and mark the most suitable idea for you.    First set up your PC's environment and start building given tutorials of the project.meanwhile, introduce yourself in t...