Wednesday, February 6, 2019

First C++ Program!

Hello C!
#include "stdio.h"

int main (int argc, char ** argv) {
    char cstr[] = "C Language";
    printf ("Hello World from %s! \n", cstr );
    return 0;
}
Hello C++!
#include <cstdio>
#include <iostream>
using namespace std;

void pr_line(), pr_message(); // prototypes

int main()
{
    printf ("Hello World\n");

    string str = "C++ Language";
    const char *cstr = str.c_str();
    printf ("Hello %s\n", cstr);
    cout << "Hello" << str << endl;

    int ia[] = { 1,2,3,4,5 };
    int x , y , z = 0;
    int numbers[10];
    cout << sizeof(numbers) << endl;
    cout << sizeof(*numbers) << endl;
    cout << sizeof(numbers[0]) << endl;
    return 0;
}
C/C++ comments
// this is comments
/* this is comments for multiple lines
*/
C++ IO
#include <iostream>
#include <fstream>
using namespace std;

int main(void)
{
    ifstream fin ("my.in");
    ofstream fout ("my.out");
    int n;
    fin >> n;
    auto b = new int[n];
    for (int = 1; i < n; i++) {
        fin >> b[2*i];
        fin >> b[2*i+1];
    }

    // 2D array
    auto ary = new int*[n];
    for ( int i=0; i < n; i++) {
        ary[i] = new int[2];
        for (int j=0; j < 2; j++) {
            fin2 >> ary[i][j];
            cout << ary[i][j] << endl;
        }
    }

}

# my.in
# 2
# 1 2
# 3 4
STL
#include <vector>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <vector>

No comments:

Post a Comment