#include <iostream>
#include <vector>
void main()
{
std::vector<bool> BoolVect;
std::vector<int> IntVect;
std::vector<__int64> Int64Vect;
std::cout << BoolVect.max_size() << std::endl;
std::cout << IntVect.max_size() << std::endl;
std::cout << Int64Vect.max_size() << std::endl;
}
//出力結果
//4294967295
//1073741823
// 536870911
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
void main()
{
std::vector<int> IntVect;
IntVect.push_back(1);
IntVect.push_back(2);
IntVect.push_back(3);
std::copy(IntVect.begin(),IntVect.end(),std::ostream_iterator<int>(std::cout, ","));
std::cout << std::endl;
}
//出力結果
1,2,3,
デフォルトコピーコンストラクタでは、
ビット単位でインスタンスのコピーが作成されます。
配列であっても、値がコピーされます。(DeepCopyと言える)
#include <stdio.h>
class TestClass{
public:
int mIntArr[3];
static const int mIntArrUB = 2;
void DoPrint(){
for(int I=0;I<=mIntArrUB;I++){
printf("mIntArr[%3d] = %3d\n",I,mIntArr[I]);
}
}
};
void main()
{
TestClass Ins1;
for(int I=0;I<=TestClass::mIntArrUB;I++){
Ins1.mIntArr[I]=I;
}
TestClass Ins2 = Ins1;
printf("Ins1.mIntArr=%p,&Ins1.mIntArr[1]=%p,&Ins1.mIntArr[2]=%p\n",
Ins1.mIntArr,Ins1.mIntArr+1,Ins1.mIntArr+2);
printf("Ins2.mIntArr=%p,&Ins1.mIntArr[1]=%p,&Ins1.mIntArr[2]=%p\n",
Ins2.mIntArr,Ins2.mIntArr+1,Ins2.mIntArr+2);
for(int I=0;I<=TestClass::mIntArrUB;I++){
Ins1.mIntArr[I]=100+I;
}
puts("---Ins1---");
Ins1.DoPrint();
puts("---Ins2---");
Ins2.DoPrint();
}
//出力結果
Ins1.mIntArr=0012FF68,&Ins1.mIntArr[1]=0012FF6C,&Ins1.mIntArr[2]=0012FF70
Ins2.mIntArr=0012FF74,&Ins1.mIntArr[1]=0012FF78,&Ins1.mIntArr[2]=0012FF7C
---Ins1---
mIntArr[ 0] = 100
mIntArr[ 1] = 101
mIntArr[ 2] = 102
---Ins2---
mIntArr[ 0] = 0
mIntArr[ 1] = 1
mIntArr[ 2] = 2
POD構造体とは、C言語の構造体と互換性を持つ構造体のことである。 PODは、Plain Old Dataの略
メソッドの中で
*this = CTestClass(500,600);
という記述は、
コンストラクタの処理と、代入演算子の処理が実行される。
#include <stdio.h>
#include <iostream>
class CTestClass{
int mA;
int mB;
int mC;
public:
//コンストラクタ
CTestClass(int pA,int pB){
mA = pA;
mB = pB;
static int cnt =1;
mC=cnt++;
std::cout << "コンストラクタを起動" << std::endl;
};
//コピーコンストラクタ
CTestClass(const CTestClass& Another){
mA = Another.mA;
mB = Another.mB;
std::cout << "コピーコンストラクタを起動" << std::endl;
};
//代入演算子
CTestClass &operator=(const CTestClass &Another){
mA = Another.mA;
mB = Another.mB;
std::cout << "代入演算子を起動" << std::endl;
return *this;
};
void TestMethod(){
std::cout << "*this = CTestClass(500,600)" << std::endl;
*this = CTestClass(500,600); //コンストラクタと代入演算子を起動
};
};
void main()
{
std::cout << "処理1■■■■■■■■■■" << std::endl;
CTestClass InsTestClass1(100,200);//コンストラクタを起動
std::cout << "処理2■■■■■■■■■■" << std::endl;
CTestClass InsTestClass2 = InsTestClass1;//コピーコンストラクタを起動
std::cout << "処理3■■■■■■■■■■" << std::endl;
CTestClass InsTestClass3(300,400);//コンストラクタを起動
InsTestClass3 = InsTestClass1; //代入演算子を起動
std::cout << "処理4■■■■■■■■■■" << std::endl;
InsTestClass1.TestMethod();
}
//出力結果
処理1■■■■■■■■■■
コンストラクタを起動
処理2■■■■■■■■■■
コピーコンストラクタを起動
処理3■■■■■■■■■■
コンストラクタを起動
代入演算子を起動
処理4■■■■■■■■■■
*this = CTestClass(500,600)
コンストラクタを起動
代入演算子を起動
#include <iostream>
#include <windows.h>
void OutDate()
{
SYSTEMTIME stTime;
GetLocalTime(&lstTime);
char strTime[100];
wsprintf(strTime , "日時は、%4d年%2d月%2d日%2d時%2d分%2d秒",
stTime.wYear , stTime.wMonth , stTime.wDay ,
stTime.wHour , stTime.wMinute , stTime.wSecond);
std::cout << strTime << std::endl;
}
#include <algorithm>
#include <stdio.h>
#include <string>
#include <vector>
int main()
{
std::vector<char> CharVect;
CharVect.push_back('a');
CharVect.push_back('b');
CharVect.push_back('c');
do{
std::string wkStr(CharVect.begin(),CharVect.end());
puts(wkStr.c_str());
} while(next_permutation(CharVect.begin(),CharVect.end()));
}
//出力結果
abc
acb
bac
bca
cab
cba