Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Basic Data Types
- #include <iostream>
- using namespace std;
- int main() {
- int a;
- long b;
- char c;
- float d;
- double e;
- e;
- cin >>a>>b>>c>>d>>e;
- cout << fixed;
- cout << a<<'\n';
- cout << b<<'\n';
- cout << c<<'\n';
- cout << d<<'\n';
- cout << e<<'\n';
- return 0;
- }
- Conditional Statements
- #include <bits/stdc++.h>
- using namespace std;
- int main()
- {
- string a[]={"Greater than 9","one","two","three","four","five","six","seven","eight","nine"};
- int n;
- cin >> n;
- if(n>9){
- cout << a[0];
- }
- else {
- cout << a[n];
- }
- return 0;
- }
- For Loop
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- string a[] = {"one", "two", "three", "four", "five",
- "six", "seven", "eight", "nine"};
- int n, c;
- cin >> n >> c;
- for (; n <= c; n++) {
- if (n > 9 && c > 9 && n % 2 == 0) {
- cout << "even" << endl;
- } else if (n > 9 && c > 9 && n % 2 != 0) {
- cout << "odd" << endl;
- } else {
- cout << a[n - 1] << endl;
- }
- }
- return 0;
- }
- Functions
- #include <iostream>
- #include <cstdio>
- using namespace std;
- int main (){
- int i;
- int a[4];
- for(i=0;i<4;i++){
- cin >> a[i];
- }
- int max=a[0];
- for(i=0;i<4;i++){
- if (a[i]>max){
- max=a[i];
- }
- }
- cout<<max;
- return 0; }
- Pointer
- # include <iostream>
- using namespace std;
- int main(){
- int a,b,c,d;
- cin >>a>>b;
- c=a+b;
- if (a>=b){
- d=a-b;}
- else{
- d=b-a;
- }
- cout<<c<<endl<<d;
- return 0;
- }
- Arrays Introduction
- #include <algorithm>
- #include <cmath>
- #include <cstdio>
- #include <iostream>
- #include <vector>
- using namespace std;
- int main() {
- int i, n;
- cin >> n;
- int a[n];
- for (i = 0; i <= n - 1; i++) {
- cin >> a[i];
- }
- for (i = n - 1; i >= 0; i--) {
- cout << a[i] << ' ';
- }
- return 0;
- }
- Variable Sized Arrays
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- int main() {
- int n,q;
- cin >> n >> q;
- vector< vector<int> > a(n);
- // input each array
- for (int i=0;i<n;i++) {
- int k;
- cin >> k;
- for (int j=0;j<k;j++) {
- int data;
- cin >> data;
- a[i].push_back(data);
- }
- }
- // do the queries
- for (int i=0;i<q;i++) {
- int x,y;
- cin >> x >> y;
- cout << a[x][y] << endl;
- }
- return 0;
- }
- StringStream
- #include <iostream>
- #include <string>
- using namespace std;
- int main() {
- string s;
- cin >> s;
- for (size_t f = 0; (f = s.find(",", f)) != string::npos;
- s.replace(f, 1, "\n"))
- ;
- cout << s << endl;
- return 0;
- }
- Strings
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- string a, b;
- cin >> a >> b;
- cout << a.length() << ' ' << b.length() << endl;
- cout << a + b << endl;
- swap(a[0], b[0]);
- cout << a << ' ' << b << endl;
- return 0;
- }
- Structs
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- struct Student { int age; string first_name; string last_name; int standard; };
- int main() { Student st; std::string str1; std::string str2; cin >> st.age; cin>>str1; cin>>str2; cin>> st.standard; st.first_name=str1.substr(0,50); st.last_name=str2.substr(0,50);
- cout << st.age << " " << st.first_name << " " << st.last_name << " " << st.standard;
- return 0;
- }
- Class
- #include <iostream>
- #include <sstream>
- using namespace std;
- class Student {
- private:
- int age;
- string first_name;
- string last_name;
- int standard;
- public:
- void set_age (int a){
- age=a;
- }
- void set_first_name (string a){
- first_name=a;
- }
- void set_last_name (string b){
- last_name=b;
- }
- void set_standard (int c){
- standard=c;
- }
- int get_age(){
- return age;
- }
- string get_first_name(){
- return first_name;
- }
- string get_last_name(){
- return last_name;
- }
- int get_standard(){
- return standard;
- }
- string to_String(){
- return to_string(age) + "," + first_name + "," + last_name + "," +
- to_string(standard);
- }
- };
- int main() {
- int age, standard;
- string first_name, last_name;
- cin >> age >> first_name >> last_name >> standard;
- Student st;
- st.set_age(age);
- st.set_standard(standard);
- st.set_first_name(first_name);
- st.set_last_name(last_name);
- cout << st.get_age() << "\n";
- cout << st.get_last_name() << ", " << st.get_first_name() << "\n";
- cout << st.get_standard() << "\n";
- cout << "\n";
- cout << st.to_String();
- return 0;
- }
- Classes and Objects
- class Student{
- int scores[5];
- public:
- void input(){
- for(int i=0; i<5; i++){
- cin >> scores[i];
- }
- }
- int calculateTotalScore(){
- int total = 0;
- for(int i=0; i<5; i++){
- total += scores[i];
- }
- return total;
- }
- };
- Box It!
- class Box{
- private:
- int l, b, h;
- public:
- Box(){
- l = 0;
- b = 0;
- h = 0;
- }
- Box(int length, int breadth, int height){
- l = length;
- b = breadth;
- h = height;
- }
- Box(const Box& B){
- l = B.l;
- b = B.b;
- h = B.h;
- }
- int getLenght(){
- return l;
- }
- int getBreadth(){
- return b;
- }
- int getHeight(){
- return h;
- }
- long long CalculateVolume(){
- return (long long)l*b*h;
- }
- friend bool operator < ( Box&A,Box& B){
- if( (A.l < B.l) || ((A.b < B.b) && (A.l == B.l)) || ((A.h < B.h) && (A.l == B.l) && (A.b == B.b)) ){
- return true;
- }else{
- return false;
- }
- };
- friend ostream& operator<< (ostream& output, const Box& B){
- output << B.l << " " << B.b << " " << B.h;
- return output;
- }
- };
- Vector-Sort
- #include <vector>
- #include <iostream>
- #include <iterator>
- #include <algorithm>
- int main() {
- std::vector<int> v;
- int n;
- std::cin >> n;
- std::copy_n(std::istream_iterator<int>(std::cin), n, std::back_insert_iterator<std::vector<int>>(v));
- std::sort(v.begin(), v.end());
- std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
- return 0;
- }
- Vector-Erase
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- int main() {
- int n;
- cin >> n;
- vector <int> v;
- int a;
- for(int i=0;i<n;i++){
- cin >> a;
- v.push_back(a);
- }
- int k;
- cin >> k;
- v.erase(v.begin()+k-1);
- int l,m;
- cin >> l >> m;
- v.erase(v.begin()+l-1,v.begin()+m-1);
- cout << v.size()<<endl;
- for(int i=0; i<v.size();i++){
- cout << v[i] << " ";
- }
- return 0;
- }
- Lower Bound-STL
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- int main() {
- /* Enter your code here. Read input from STDIN. Print output to STDOUT */
- int m, num;
- cin >> m;
- vector<int> v;
- for (int i=0; i<m; i++){
- cin >> num;
- v.push_back(num);
- }
- int n, val;
- cin >> n;
- for (int i=0; i<n; i++){
- cin >> val;
- vector<int>::iterator low = lower_bound(v.begin(), v.end(), val);
- if (v[low - v.begin()] == val)
- cout << "Yes " << (low - v.begin()+1) << endl;
- else
- cout << "No " << (low - v.begin()+1) << endl;
- }
- return 0;
- }
- Sets-STL
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <set>
- #include <algorithm>
- using namespace std;
- int main() {
- int iCount;
- set<int> ss;
- cin >> iCount;
- for (int i=0; i<iCount; ++i){
- int type, query;
- cin >> type >> query;
- switch (type){
- case 1:
- ss.insert(query);
- break;
- case 2:
- ss.erase(query);
- break;
- case 3:
- cout << (ss.find(query) == ss.end() ? "No" : "Yes") << endl;
- break;
- }
- }
- return 0;
- }
- Maps-STL
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <set>
- #include <map>
- #include <algorithm>
- using namespace std;
- int main() {
- int q, type; cin >> q;
- map<string,int> clas; string name;
- for (int i(0), mark; i<q; ++i)
- {
- cin >> type >> name;
- if (type == 1){
- cin >> mark;
- clas[name] += mark;
- }
- else if (type == 2)
- clas.erase(name);
- else
- cout << clas[name] << "\n";
- }
- return 0;
- }
- Print Pretty
- cout << hex << showbase << nouppercase<<left;
- cout << (long long)A << endl;
- cout << setw(15) << showpos << setfill('_') << setprecision(2)<<fixed<<right;
- cout << B << endl;
- cout << scientific << setw(15) << setprecision(9) << uppercase<<noshowpos;
- cout << C << endl;
- Inheritance Introduction
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- class Triangle{
- public:
- void triangle(){
- cout<<"I am a triangle\n";
- }
- };
- class Isosceles : public Triangle{
- public:
- void isosceles(){
- cout<<"I am an isosceles triangle\n";
- }
- void description(){
- cout<<"In an isosceles triangle two sides are equal\n";
- }
- };
- int main(){
- Isosceles isc;
- isc.isosceles();
- isc.description();
- isc.triangle();
- return 0;
- }
- Rectangle Area
- class Rectangle
- {
- public:
- Rectangle() = default;
- virtual void display() const { std::cout << _width << " " << _height << std::endl; }
- int area() const { return _width * _height; }
- protected:
- int _width = 0;
- int _height = 0;
- };
- class RectangleArea : public Rectangle
- {
- public:
- RectangleArea() = default;
- virtual void display() const override { std::cout << area() << std::endl; }
- void read_input() { std::cin >> _width >> _height; }
- };
- Multi Level Inheritance
- #include <iostream>
- #include <memory>
- class Shape
- {
- public:
- virtual const void triangle()const = 0;
- virtual ~Shape(){ }
- };
- class Triangle: public Shape
- {
- public:
- virtual const void triangle()const override
- { std::cout<<"I am a triangle\n"; }
- virtual ~Triangle(){ }
- };
- class Isosceles : public Triangle
- {
- public:
- virtual const void triangle()const override
- { std::cout<<"I am an isosceles triangle\n"; }
- virtual ~Isosceles(){ }
- };
- class Equilateral: public Isosceles
- {
- public:
- virtual const void triangle()const override
- { std::cout<<"I am an equilateral triangle\n"; }
- virtual ~Equilateral(){ }
- };
- int main()
- {
- std::shared_ptr<Shape> obj[3];
- obj[0] = std::make_shared<Equilateral>();
- obj[1] = std::make_shared<Isosceles>();
- obj[2] = std::make_shared<Triangle>();
- for(auto it: obj)
- it->triangle();
- return 0;
- }
- C++ Class Templates
- template <class T> class AddElements {
- public:
- T element;
- AddElements(T i) {element = i;}
- T add(T i) {return element+i;}
- private:
- };
- template <> class AddElements <string> {
- public:
- string element;
- AddElements(string i) {element = i;}
- string concatenate(string element2) {return element+element2;}
- private:
- };
- Overload Operators
- ostream &operator<<(ostream &os, const Complex &c) {
- return os << c.a << (c.b > 0 ? '+' : '-') << 'i' << c.b;
- }
- Complex operator+(const Complex &a, const Complex &b) {
- return {a.a + b.a, a.b + b.b};
- }
- //Overload operators + and << for the class complex
- //+ should add two complex numbers as (a+ib) + (c+id) = (a+c) + i(b+d)
- //<< should print a complex number in the format "a+ib"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement