C++中 auto自动变量,命名空间,using作用以及作用域
1.auto關鍵字的用途
A:自動變量,可以自動獲取類型,輸出,類似泛型
B:自動變量,可以實現(xiàn)自動循環(huán)一維數(shù)組
C:自動循環(huán)的時候,對應的必須是常量
2.auto自動變量,自動匹配類型的案例如下:
注意:如果是在QT下運行上面的的程序需要加上C++11的相關配置(CONFIG += C++11)
3.通過auto關鍵字自動循環(huán)一維數(shù)組的案例
#include<iostream>
#include<stdlib.h>
#include<iomanip>
usingnamespacestd;
voidmain()
{
???//定義一維數(shù)組,下面的數(shù)組名是一個指針常量
???intnum[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
???//通過循環(huán)遍歷出數(shù)組
???for (autodata :num)
???{
???????cout <<setw(5) <<data;
???}
???cout <<endl;
?
???//當變成二維數(shù)組之后,不能直接通過auto來輸出
???//auto自動循環(huán)begin?endl;必須是一個數(shù)組的常量
???doublenum2[2][5] = { 1.0, 2.0, 3.0, 4.5, 5, 6, 7, 8, 9, 10 };
???for (autodata :num2)//泛型C++語法,循環(huán)一維數(shù)組,是個常量
???{
???????cout <<data <<std::endl;
???????for (inti = 0;i < 5;i++)
???????{
???????????std::cout<<setw(5) << *(data +i);
???????}
???????cout <<endl;
???}
???system("pause");
}
運行結果如下:
4.關于頭文件
在C++中,為了區(qū)分C++,C++中不使用.h文件頭
5.C++注重類型,它是一種強類型語言,嚴格檢查類型。
6.C++中的輸出函數(shù),寬字符輸出,賦初值的方式,輸出符,::域控制符
#include<iostream>??//在C++中,為了區(qū)分C++,C++中不使用.h文件頭
#include<stdlib.h>
usingnamespacestd;
?
voidmain()
{
???inta = 5;
???//C++中賦值:A變量名(數(shù)值)?B:變量名=數(shù)值,通過這兩種方式。
???intb(5);
???cout <<a <<" " <<b <<endl;
???//通過括號賦值
???doublec(3.5);
???cout <<c <<endl;
???//通過等號賦值
???char *pStr ="1234";
???cout << *pStr <<" " <<pStr <<endl;
???//通過括號賦值
???char *str("china");
???cout << *str <<" " <<str <<endl;
???//寬字符,漢子,棒子文
???wchar_t *str2(L"china");
???wcout << *str2 <<" " <<str2 <<endl;
?
???system("pause");
}
#include<iostream>
?
voidmain()
{
???//輸出符 <<
???//std命名空間
???//::域控制符
???std::cout << "hello world";
?
???system("pause");
}
7.一個文件中使用另外一個文件中聲明的變量的時候,C++要求顯示的加上extern關鍵字調用這個全局變量。
8.調用命名空間中的變量已經(jīng)命名空間中的函數(shù)
A:有名命名空間
#include"iostream"
#include"stdlib.h"
?
namespacemyspace
{
???inta(15);
???voidprint()
???{
???????std::cout << "鋤禾日當午" << std::endl;
???}
}
?
namespacemyspaceA
{
???inta(25);
???voidprint()
???{
???????std::cout << "AAAAAAAAAAAA" <<std::endl;
???}
}
?
voidmain()
{
???inta(5);
???//如果想不被上面的a變量影響,而直接訪問命名空間
???//中的變量,這里要加上命名空間名
???std::cout << "myspace a = " <<myspace::a << std::endl;
???//調用myspaceA中定義的變量a
???std::cout << "myspaceA a = " <<myspaceA::a << std::endl;
???std::cout << "main a = " <<a <<std::endl;
?
???std::cout << std::endl;
???//調用命名空間中的函數(shù)
???myspace::print();
???myspaceA::print();
?
???system("pause");
}
運行結果如下:
B:無名命名空間
#include"iostream"
//沒有命名的命名空間中的參數(shù)可以直接調用
namespace
{
???inta(3);
???voidprint()
???{
???????std::cout << "gogogo";
???}
}
?
voidmain()
{
???//說明沒有命名的命名空間可以直接調用a
???std::cout << a <<std::endl;
???print();
???getchar();
}
輸出結果是:
3
gogogo
C:命名空間中所有的數(shù)據(jù),函數(shù),類,對象都是共有的,結構體內部的默認是共有的。
案例如下:
#include<iostream>
?
//namespace所有數(shù)據(jù),函數(shù),類,對象都是共有
namespacerunmove
{
???inty(5);
???int(*padd)(int,int);//函數(shù)指針接口
???inty1(5);
???classmyclass
???{
???????//類中的變量等默認是私有的
???public:
???????inta;
???};
}
?
//對應上面的函數(shù)指針
intadd(inta,intb)
{
???returna +b;
}
?
intaddp(inta,intb)
{
???std::cout << a <<" " <<b <<std::endl;
???returna +b;
}
?
//結構體默認是透明的(public的)
structMyStruct
{
???intb;
private:
???inta;//是私有
};
?
voidmain()
{
???//namespace所有數(shù)據(jù),函數(shù),類,對象都是共有
???MyStructstruct1;//結構體內部默認公有
???struct1.b = 20;
???std::cout << struct1.b << std::endl;
?
???std::cout << runmove::y << " " <<runmove::y1 << std::endl;
???runmove::padd = addp;
???std::cout << runmove::padd(5, 6) << std::endl;
???getchar();
}
9.使用using關鍵字的時候,必須在命名空間的后面
關于作用域:
A:在函數(shù)體內使用using的時候,using的作用域是從using這行代碼開始到函數(shù)體結束。
B:函數(shù)體外使用using的時候,作用域是從using這行代碼開始到文件本文件結尾。
案例說明:
#include<iostream>
#include<stdlib.h>
?
namespacemydata
{
???inta(6);
}
?
namespaceyourdata
{
???inta(8);
}
?
//如果想使用mydata這個明明空間,并且使用using關鍵字,這時候要把using放
//在mydata命名空間后面
usingnamespacemydata;
usingnamespaceyourdata;
?
//using如果變量重名,會出現(xiàn)不明確錯誤,加上命名空間修飾符
voidgo()
{
???//命名空間如果在塊語句內部,作用域是定義開始到語句結束
???std::cout << mydata::a << std::endl;
}
?
//using namespacemydata;//作用域為從代碼開始到結束
voidmain()
{
???std::cout << mydata::a << std::endl;
???std::cout << yourdata::a << std::endl;
?
???system("pause");
}
10.命名空間的嵌套,為命名空間起別名,命名空間的拓展
#include<iostream>
namespacerunrunrunrun
{
???inta(10);
???char *str("gogogo");
???namespacerun??//命名空間的嵌套
???{
???????inta(9);
???}
}
//命名空間的拓展
namespacerunrunrunrun
{
???int?y(5);
???//int?a(15);重定義錯誤
}
//注意這里的r就是別名,而不是后者
namespacer =runrunrunrun;//給命名空間起一個別名
?
voidmain()
{
???//命名空間可以嵌套
???std::cout << r::run::a << std::endl;
???std::cout << r::y << std::endl;
???std::cout << r::a << std::endl;
???system("pause");
}
11.關于默認參數(shù)問題
??A:默認參數(shù)必須放在右邊
??B:默認參數(shù)中間不允許出現(xiàn)不默認的
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
?
//默認參數(shù)必須放在右邊
//默認參數(shù)中間不允許出現(xiàn)不默認的
voidprint(intc,inta = 1,intd = 2,intb = 3)
{
???std::cout << a <<b <<c <<std::endl;
}
?
voidprintf(doublec)
{
???std::cout << "hello,world" <<std::endl;
}
?
voidmain()
{
???//print(1,2,3);
???//函數(shù)指針沒有默認參數(shù),必須全部輸入數(shù)據(jù)
???void(*pt1)(intc,inta,intd,intb) =print;
???pt1(100,1,2,3);//函數(shù)指針調用,沒有用默認的
???print(100);
?
???system("pause");
}
總結
以上是生活随笔為你收集整理的C++中 auto自动变量,命名空间,using作用以及作用域的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言文件操作函数的编写
- 下一篇: 引用内部函数绑定机制,R转义字符,C++