フロントエンドデベロッパーのメモ

スキル: HTML/Jade/Jinja2, CSS/SCSS, JavaScript(ES6), Angular, React,Next, Redux, Node, Express, Python, Flask, Postgres, Redis, MongoDB, Kafka, Knex, SQLAlchemy, React Native, EXPO, GraphQL/Apollo, REST, AWS, Heroku, Docker, CircleCI, SCRUM, XP, Vim, TDD

C++でパターン出力

English version is below. アルゴリズム設計力を磨くために読み始めた本「プログラマの考え方が面白いほど身につく本」の問題例がC++で書かれているので、C++の習得も含めて今日学んだことを書いておきます。

C++で書かれたファイルをターミナルで出力するには事前にコンパイルする必要があります。

Run a C/C++ program on terminal using gcc compiler « Learn and Try の情報によると、 コンパイル方法は2種類ある見たいです。

$ sudo g++ hello.cpp 

Or

$ sudo g++ -o hello hello.cpp

2パターン目でhelloが2回登場していますが、ファイル名とファイルパスを示しているんでしょうか。そして、ファイル名は省略することもできる、といったところでしょうか。

コンパイルしたあとはプログラムを実行するだけ

$ ./a.out (コンパイル時に1パターン目を使用した場合)

Or

$ ./hello (コンパイル時に2パターン目を使用した場合)

これで以下のコードを走らせて見ました。

#include <iostream>
using std::cin;
using std::cout;

int main()
{
    for (int row = 1; row <= 7; row++)
    {
        for (int hashNum = 1; hashNum <= 4 - abs(4 - row); hashNum++)
        {
            cout << "#";
        }
        cout << "\n";
    }
}

出力結果は、以下の山です。

#
##
###
####
###
##
#

この本では、今まで直面したことのない未知の問題でも過去の経験則から類似点を見つけ出すことで問題を解決する能力を高める秘訣や練習課題が準備されています。それらの問題を通して、プログラマと同様の考え方を身につけるという本です。タイトルに書いてることそのまんまですが。

読んでて面白いですし、C++のコーディングスキルも磨けそうです。 また、今日ぎプログラミングに参加していて気づいたことですが、トッププログラマーたちの回答はC++で書かれていることが多いため、C++でコードが書けるようになればトッププログラマーたちのコードも読めるようになって、さらにスキルアップできて、一石二鳥です。

Hello there! I have started to read awesome Japanese book that named 「プログラマの考え方が面白いほど身につく本」. The title means the book you can acquire the way of thinking a programmer does. That makes me excited reading this title! However, the examples are written in C++, so I started to learn C++ through this book. What I have learned today is how to compile a file and how to run C++ program on terminal.

I have referred the ways from the site below. Run a C/C++ program on terminal using gcc compiler « Learn and Try

Since C++ program is needed to compile before running, you have two methods to do that.

$ sudo g++ hello.cpp 

Or

$ sudo g++ -o hello hello.cpp

In the second method, you type hello twice. And first one means the file name and the second one is to show the file path I guess.

After compiling successfully, you can run the program with two methods.

$ ./a.out (if you compiled with the first method)

Or

$ ./hello (if you compiled with the second method)

Now you can see the result, awesome!

In addition, I will share the code that I have run.

#include <iostream>
using std::cin;
using std::cout;

int main()
{
    for (int row = 1; row <= 7; row++)
    {
        for (int hashNum = 1; hashNum <= 4 - abs(4 - row); hashNum++)
        {
            cout << "#";
        }
        cout << "\n";
    }
}

The result is...

#
##
###
####
###
##
#

As long as I have joined in a programming contest, the most of the top programmers use C++ as a main language. So learning C++ through this book helps me to upgrade my problem solving ability and reading ability of top programmers' codes!! I can learn many things with this book! By the way, I hope this helps other non-C++ programmer to know how to run your C++ code!