C++学習ログ16--現在のC++バージョンを取得し、名前空間を設定し、参照します


1.現在のC++バージョン番号を取得します

int main()
{
    
    
    if (__cplusplus == 201703L) std::cout << "C++17\n";
    else if (__cplusplus == 201402L) std::cout << "C++14\n";
    else if (__cplusplus == 201103L) std::cout << "C++11\n";
    else if (__cplusplus == 199711L) std::cout << "C++98\n";
    else std::cout << "pre-standard C++\n";

    std::cin.get();  //保留命令窗口

    return 0;
}

ここに画像の説明を挿入

結局、私のコンパイラはC++17バージョンをサポートしています。
(ソリューションを右クリックすると、プロパティで変更できます)

次に、名前空間を設定します

#include <iostream>
using std::cin;
namespace NS1
{
    
    
    int x = 1;
};
namespace NS2
{
    
    
    int x = 2;
};
using std::cout;
using std::endl;
using std::string;
int main()
{
    
    
    cout << NS1::x<<endl;
    cout << NS2::x << endl;
    cin.get();
    return 0;
}

ここに画像の説明を挿入

これにより、独自の名前空間NS1およびNS2が作成されます。

3.C++での参照


#include <iostream>

int main()
{
    
    
    int x = 0;
    int y{
    
     10 };
    int& rx = x;
    rx = 8;

    const char* s = "hello";
    const char* t = "world";
    
    const char*& r = s;
    std::cout << r << std::endl;

    std::cin.get();
    return 0;

   
}

ここに画像の説明を挿入


参照型のパラメーターでは、番号の順序が入れ替わります。(参照はポインターとして理解できます)


#include <iostream>
using std::cout;
using std::endl;
using std::cin;
int swap(int& a, int& b, int& c)
{
    
    
	int tempa = a;
	int tempb = b;
	int tempc = c;
	a = tempc;
	b = tempa;
	c = tempb;
	int back = 0;
	back = (a >= b ? a : b);
	back = (back >= c ? back : c);
	return back;
}
int main()
{
    
    
	int a = 0;
	int b = 0;
	int c = 0;

	cin>> a;
	cout << "a  =  ";
	cout << a << endl;

	cin>> b;
	cout << "b  =  ";
	cout << b << endl;

	cin>> c;
	cout << "c  =  ";
	cout << c << endl;

	int getreturn = 0;
	getreturn=swap(a, b, c);

	cout << "a  =  " ;
	cout << a << endl;

	cout << "b  =  " ;
	cout << b << endl;

	cout << "c  =  " ;
	cout << c << endl;

	cout << "getreturn =  " ;
	cout << getreturn << endl;

	cin.get();
	return 0;
}

ここに画像の説明を挿入
結果は上記のとおりです。

おすすめ

転載: blog.csdn.net/taiyuezyh/article/details/124106529