一、二维数组
#include <iostream>
#include<windows.h>
#include <map>
using namespace std;
void test1()
{
map<int, map<string,float>> m;
m[0]["sch"] = 1.1;
m[1]["hsd"] = 2.2;
for (map<int, map<string,float>>::iterator it = m.begin(); it != m.end();it++)
{
for ( map<string, float>::iterator it2= it->second.begin();
it2 != it->second.end();it2++)
{
cout << it2->first<<" : "<<it2->second<<" ";
}
cout << endl;
}
}
void test2()
{
map<int, string> m;
m[0]= "sch";
m[1]= "hsd";
for (map<int, string>::iterator it = m.begin(); it != m.end();it++)
{
cout << it->first <<" "<<it->second<< endl;
}
}
void test3()
{
array<int ,6> a={1,2,3,4,5,6};
cout<< a.size()<< endl;
for(unsigned int i(0);i < a.size(); i++){
cout<< a[i]<< endl;
}
}
int main() {
SetConsoleOutputCP ( CP_UTF8 );
test1();
system("pause");
return 0;
}
二、私有成员
#include <iostream>
#include<windows.h>
#include <map>
using namespace std;
class myclass{
public:
struct Store{
int id;
int value;
bool status;
};
void test6(int number);
private:
Store a;
};
void my_print(const myclass::Store &store){
cout<<"store.id == "<<store.id<<endl;
cout<<"store.status == "<<store.status<<endl;
cout<<"store.value == "<<store.value<<endl;
cout<<"-------------------------------------------"<<endl;
}
void myclass::test6(int number){
if(number==0){
a.id=0;
a.value=0;
a.status=false;
}else{
a.id =1;
a.status=true;
a.value =number;
}
myclass::Store & s= a;
cout<< "a的地址== "<<&a<<endl;
my_print(s);
}
int main() {
SetConsoleOutputCP ( CP_UTF8 );
myclass c;
c.test6(0);
c.test6(1);
c.test6(0);
system("pause");
return 0;
}
三、function用法
#include <iostream>
#include<windows.h>
#include <functional>
using namespace std;
class A
{
std::function<void(void)> callback_;
public:
A(const std::function<void(void)>& f):callback_(f)
{}
void notify()
{
callback_();
}
};
class Foo
{
public:
void operator()(void)
{
std::cout << __FUNCTION__ << std::endl;
}
};
void call_when_even(int x, const std::function<void(int)>& f)
{
if (x % 2 == 0) {
f(x);
}
else {
f(0);
}
}
void output(int x)
{
std::cout<< x << ""<<endl;
}
int main() {
SetConsoleOutputCP ( CP_UTF8 );
Foo foo;
A aa(foo);
aa.notify();
call_when_even(1,output);
system("pause");
return 0;
}
四、类里面创建另一个类
#include <iostream>
#include<windows.h>
#include <map>
#include <functional>
using namespace std;
class A
{
public:
A()
{
cout<<"A 构造"<<endl;
}
A(int a,int b)
{
cout<<a<<endl;
cout<<b<<endl;
}
int a;
~A()
{
cout<<"A 析构"<<endl;
}
};
class C
{
public:
C()
{
cout<<"C 构造"<<endl;
}
C(int a,int b)
{
cout<<a<<endl;
cout<<b<<endl;
}
int a;
~C()
{
cout<<"C 析构"<<endl;
}
};
class B{
public:
B()
{
cout<<"B 构造"<<endl;
}
~B()
{
cout<<"B 析构 "<<endl;
}
private:
A a;
public:
C c;
};
void test()
{
B b;
}
int main() {
SetConsoleOutputCP ( CP_UTF8 );
if(-1)
test();
system("pause");
return 0;
}
五、lambda
#include <iostream>
#include<windows.h>
#include <map>
#include <functional>
using namespace std;
using fun_name=std::function<void(int b,int c)>;
void fun(int a, std::function<void(int b,int c)> f)
{
cout <<__FUNCTION__ << ": "<< a <<endl;
fun_name d=f;
cout <<__FUNCTION__ << " 内的 f 函数"<<endl;
f(4,6);
cout <<__FUNCTION__ << " 内的 d 函数"<<endl;
d(90,9);
}
void fun2(int b, int c)
{
cout <<__FUNCTION__<< ": "<<"b+c = "<<b+c <<endl;
}
void test()
{
int g =100;
fun( 1,[&](int d, int e){
g++;
fun2(d,e);
cout <<__FUNCTION__<< ": "<<"g = "<<g <<endl;
}
);
}
int main() {
SetConsoleOutputCP ( CP_UTF8 );
test();
system("pause");
return 0;
}
结果打印:
fun: 1
fun 内的 f 函数
fun2: b+c = 10
operator(): g = 101
fun 内的 d 函数
fun2: b+c = 99
operator(): g = 102
六、Map动态申请
#include <iostream>
#include<windows.h>
#include <map>
#include <functional>
using namespace std;
enum mGroup{
GROUP_1=0,
GROUP_2=1,
GROUP_3=2,
GROUP_4=3,
GROUP_5=4,
GROUP_6=5,
};
class A{
public:
struct Store{
int id;
array<int,8>data;
bool status;
};
A(){cout <<"A 构造"<<endl;}
~A(){cout <<"A 析构"<<endl;}
void initStore();
private:
map<mGroup,map<int, Store>> m_map;
};
void A::initStore(){
m_map[GROUP_1][0].id = 1;
m_map[GROUP_1][1].data.fill(0x02);
for(int i=0; i<5; i++){
cout << m_map[GROUP_1].size() << " ID ="<<m_map[GROUP_1][i].id <<" :" ;
for (int j = 0; j < 8 ; j++){
cout<<m_map[GROUP_1][i].data[j]<<" ";
}
cout<<endl;
}
}
int main() {
SetConsoleOutputCP ( CP_UTF8 );
A a;
a.initStore();
system("pause");
return 0;
}
运行结果:
A 构造
2 ID =1 :0 0 0 0 0 0 0 0
2 ID =0 :2 2 2 2 2 2 2 2
2 ID =0 :0 0 0 0 0 0 0 0
3 ID =0 :0 0 0 0 0 0 0 0
4 ID =0 :0 0 0 0 0 0 0 0
七、存储类方法
#include <iostream>
#include<windows.h>
#include <map>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <memory>
using namespace std;
class Parent{
public:
Parent(string name):mname(name){cout << " parent " << __FUNCTION__<<endl;}
virtual void Init()=0;
virtual void send()=0;
void set_notify_name(string notify_name){
this->notify_name=notify_name;
cout << " parent " << __FUNCTION__<<endl;
}
string name() const{
return mname;
}
protected:
string notify_name;
private:
string mname;
};
class Son1 : public Parent{
public:
Son1(string name):Parent(name){
cout << " son1 " << __FUNCTION__<<endl;
}
void Init() override{
cout << " son1 " << __FUNCTION__<<endl;
}
void send() override{
cout << " son1 " << __FUNCTION__<<endl;
}
};
class Son2 : public Parent{
public:
explicit Son2(string name):Parent(name){cout << " son2 " << __FUNCTION__<<endl;}
void Init() override{
cout << " son2 " << __FUNCTION__<<endl;
}
void send() override{
cout << " son2 " << __FUNCTION__<<endl;
}
};
map<string, shared_ptr<Parent>> mMap;
void test2(){
std::shared_ptr<Parent> transform = nullptr;
transform = std::shared_ptr<Parent>(new Son1("SVN"));
mMap[transform->name()]=transform;
transform = std::shared_ptr<Parent>(new Son2("Socket"));
mMap[transform->name()]=transform;
for(auto && item : mMap){
item.second->Init();
item.second->send();
}
char temp[16];
float i=3.1415926;
snprintf(temp, sizeof(temp),"P %.7f",i);
cout<<temp<<endl;
}
int main() {
SetConsoleOutputCP ( CP_UTF8 );
test2();
system("pause");
return 0;
}
输出结果:
parent Parent
son1 Son1
parent Parent
son2 Son2
son1 Init
son1 send
son2 Init
son2 send
P 3.1415925
八、map_struct
1.map_struct
#include <iostream>
#include<windows.h>
#include <map>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <memory>
void add(){
std::cout << __FUNCTION__ << std::endl;
}
void sub(){
std::cout << __FUNCTION__ << std::endl;
}
void sum(){
std::cout << __FUNCTION__ << std::endl;
}
struct VnsTest{
std::string cmdName;
uint16_t cmdIndex;
void (*func)();
};
std::map<int,struct VnsTest> myMap;
struct VnsTest g_VnsTest[]={
{"ADD",10,add},
{"SUB",11,sub},
{"SUM",30,sum},
};
void print_help(){
printf("********help********\n");
printf("[0]-------help\n");
printf("[-1]-------quit\n");
for(auto &&subMap : myMap){
std::cout <<subMap.first<<"\t"<<subMap.second.cmdName<< std::endl;
}
return;
}
int getLenth(){
return sizeof(g_VnsTest)/sizeof(struct VnsTest);
}
int geyl(struct VnsTest *vns,int len){
for(int i=0;i<len;i++){
std::cout<<vns[i].cmdName<<" "<<vns[i].cmdIndex<<" "<<vns[i].func<<std::endl;
}
return 0;
}
bool mstruct_add_mymap(int len){
if(len==0)
return false;
for(int i=0;i<len;++i){
myMap.insert(std::make_pair(g_VnsTest[i].cmdIndex,g_VnsTest[i]));
}
return true;
}
bool print_map(){
if(myMap.empty()){
std::cout << "myMap为空" << std::endl;
return false;
}
for(auto &&subMap : myMap){
std::cout <<"cmd="<<subMap.first << " Name=" <<subMap.second.cmdName << std::endl;
}
return true;
}
bool do_command(int input){
auto iter = myMap.find(input);
if(iter!= myMap.end()){
iter->second.func();
return true;
}
printf("Parameter is not within the range\n");
return false;
}
int main() {
SetConsoleOutputCP ( CP_UTF8 );
int len = getLenth();
if(!mstruct_add_mymap(len))
return -1;
if(!print_map())
return -1;
print_help();
while(1){
int intput = 0;
printf("cmd\n");
scanf("%d",&intput);
if(intput==0){
print_help();
}else if(intput==-1){
break;
}else {
bool ret=do_command(intput);
if(!ret) {
print_help();
}
}
}
system("pause");
return 0;
}
2.map_struct
#include <iostream>
#include <windows.h>
#include <map>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <memory>
void add() {
std::cout << __FUNCTION__ << std::endl;
}
void sub() {
std::cout << __FUNCTION__ << std::endl;
}
void sum() {
std::cout << __FUNCTION__ << std::endl;
}
struct VnsTest {
std::string cmdName;
void (*func)();
};
std::map<int, VnsTest> vnsTestMap={
{101,{"ADD",add}},
{102,{"SUB",sub}},
{103,{"SUM",sum}},
};
void printHelp() {
printf("********help********\n");
printf("[0]-------help\n");
printf("[-1]-------quit\n");
for (auto &&subMap : vnsTestMap) {
std::cout << subMap.first << "\t" << subMap.second.cmdName << std::endl;
}
return;
}
bool printMap()
{
if (vnsTestMap.empty()) {
std::cout << "myMap为空" << std::endl;
return false;
}
for (auto &&subMap : vnsTestMap) {
std::cout << "cmd=" << subMap.first << " Name=" << subMap.second.cmdName << std::endl;
}
return true;
}
bool doCommand(int input) {
auto iter = vnsTestMap.find(input);
if (iter != vnsTestMap.end()) {
iter->second.func();
return true;
}
printf("Parameter is not within the range\n");
return false;
}
int main() {
SetConsoleOutputCP(CP_UTF8);
if (!printMap())
return -1;
printHelp();
while (1) {
int intput = 0;
printf("cmd: ");
scanf("%d", &intput);
if (intput == 0) {
printHelp();
} else if (intput == -1) {
break;
} else {
bool ret = doCommand(intput);
if (!ret) {
printHelp();
}
}
}
system("pause");
return 0;
}
3.map_struct
#include <iostream>
#include <windows.h>
#include <map>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <memory>
#include <vector>
struct myData {
double originData[6];
double resultData[6];
bool resultValue;
};
class Mainwindon{
public:
Mainwindon();
void initMap();
std::map<int, myData> const getMyMap();
private:
std::map<int, myData> myMap;
};
Mainwindon::Mainwindon(){
Mainwindon::initMap();
}
void Mainwindon::initMap(){
myMap[0]={{0,0,0,0,0,0},{0,0,0,0,0,0},0};
myMap[1]={{1,2,3,4,5,6},{1,1,1,1,1,1},0};
myMap[2]={{6,5,4,3,2,1},{2,2,2,2,2,2},1};
myMap[3]={{1,2,3,4,5,6},{3,3,3,3,3,3},0};
}
std::map<int, myData> const Mainwindon::getMyMap(){
return myMap;
}
void test(){
Mainwindon mainwindon;
for(auto subMap : mainwindon.getMyMap()){
std::cout << "****************************************" <<std::endl;
std::cout <<"Index : " <<subMap.first << std::endl<<"OriginData : ";
for(auto i : subMap.second.originData ){
std::cout << i <<" ";
}
std::cout << std::endl<<"ResultData : ";
for(auto i : subMap.second.resultData ){
std::cout << i << " ";
}
std::cout << std::endl<<"Value : ";
std::cout << subMap.second.resultValue << std::endl;
std::cout << "****************************************" <<std::endl;
}
}
int main(){
SetConsoleOutputCP(CP_UTF8);
test();
system("pause");
return 0;
}
输出结果:
****************************************
Index : 0
OriginData : 0 0 0 0 0 0
ResultData : 0 0 0 0 0 0
Value : 0
****************************************
****************************************
Index : 1
OriginData : 1 2 3 4 5 6
ResultData : 1 1 1 1 1 1
Value : 0
****************************************
****************************************
Index : 2
OriginData : 6 5 4 3 2 1
ResultData : 2 2 2 2 2 2
Value : 1
****************************************
****************************************
Index : 3
OriginData : 1 2 3 4 5 6
ResultData : 3 3 3 3 3 3
Value : 0
****************************************
九、温室
#include <iostream>
#include <windows.h>
#include <map>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <memory>
struct chickenMessage{
int time;
int fanMax;
int fanMin;
int tempMax;
int tempMin;
int humMax;
int humMin;
};
class Chicken{
public:
int send(int type);
int send(int type,int number);
bool print_struct(struct chickenMessage *messages);
struct chickenMessage * getStruct(bool type);
void qtime();
private:
struct chickenMessage sendMessage[4];
};
struct chickenMessage * Chicken::getStruct(bool type){
static struct chickenMessage message[4];
if(type == 0){
message[0].time = 1;
message[0].fanMax = 2;
message[0].fanMin = 3;
message[0].tempMax = 4;
message[0].tempMin = 5;
message[0].humMax = 6;
message[0].humMin = 7;
message[1].time = 11;
message[1].fanMax = 22;
message[1].fanMin = 33;
message[1].tempMax = 44;
message[1].tempMin = 55;
message[1].humMax = 66;
message[1].humMin = 77;
message[2].time = 111;
message[2].fanMax = 222;
message[2].fanMin = 333;
message[2].tempMax = 444;
message[2].tempMin = 555;
message[2].humMax = 666;
message[2].humMin = 777;
message[3].time = 1111;
message[3].fanMax = 2222;
message[3].fanMin = 3333;
message[3].tempMax = 4444;
message[3].tempMin = 5555;
message[3].humMax = 6666;
message[3].humMin = 7777;
}
else {
message[0].time = 1111;
message[0].fanMax = 2222;
message[0].fanMin = 3333;
message[0].tempMax = 4444;
message[0].tempMin = 5555;
message[0].humMax = 6666;
message[0].humMin = 7777;
message[1].time = 111;
message[1].fanMax = 222;
message[1].fanMin = 333;
message[1].tempMax = 444;
message[1].tempMin = 555;
message[1].humMax = 666;
message[1].humMin = 777;
message[2].time = 11;
message[2].fanMax = 22;
message[2].fanMin = 33;
message[2].tempMax = 44;
message[2].tempMin = 55;
message[2].humMax = 66;
message[2].humMin = 77;
message[3].time = 1;
message[3].fanMax = 2;
message[3].fanMin = 3;
message[3].tempMax = 4;
message[3].tempMin = 5;
message[3].humMax = 6;
message[3].humMin = 7;
}
return message;
}
bool Chicken::print_struct(struct chickenMessage *messages){
for(int i=0; i<4; i++){
std::cout << messages[i].time << std::endl;
std::cout << messages[i].fanMax << std::endl;
std::cout << messages[i].fanMin << std::endl;
std::cout << messages[i].tempMax << std::endl;
std::cout << messages[i].tempMin << std::endl;
std::cout << messages[i].humMax << std::endl;
std::cout << messages[i].humMin << std::endl;
std::cout << "*************************************"<<std::endl;
}
return true;
}
bool flag=0;
void Chicken::qtime(){
static bool num=0;
static int numA=1;
static int numB=1;
static int diffTime=0;
static int histTime;
if(flag==1 && num==0){
histTime=0;
num++;
}else if(flag==0){
num =0;
numA=numB=1;
return ;
}
diffTime = 1000 - histTime;
if(numA==1&&getStruct(0)[0].time < diffTime){
send(0,1);
numA++;
}else if(numA==2&&getStruct(0)[1].time < diffTime){
send(0,2);
numA++;
}else if(numA==3&&getStruct(0)[2].time < diffTime){
send(0,3);
numA++;
}else if(numA==4&&getStruct(0)[3].time < diffTime){
send(0,4);
numA++;
}
if(numB==1&&getStruct(1)[0].time < diffTime){
send(1,1);
numB++;
}else if(numB==2&&getStruct(1)[1].time < diffTime){
send(1,2);
numB++;
}else if(numB==3&&getStruct(1)[2].time < diffTime){
send(1,3);
numB++;
}else if(numB==4&&getStruct(1)[3].time < diffTime){
send(1,4);
numB++;
}
if(5==numA)
numA=1;
if(5==numB)
numB=1;
}
int Chicken::send(int type){
if(type == 0){
std::cout << " A "<<std::endl;
print_struct(getStruct(type));
}else if(type == 1){
std::cout << " B "<<std::endl;
print_struct(getStruct(type));
}else if(type == 2){
std::cout << " A 手动 "<<std::endl;
}else if(type == 3){
std::cout << " B 手动 "<<std::endl;
}
return 0;
}
int Chicken::send(int type,int number){
if(type == 0){
std::cout << " A "<<std::endl;
std::cout<< getStruct(type)[number-1].time<< std::endl;
}else if(type == 1){
std::cout << " B "<<std::endl;
std::cout<< getStruct(type)[number-1].time<< std::endl;
}
return 0;
}
int main() {
SetConsoleOutputCP(CP_UTF8);
Chicken chicken;
chicken.send(0);
chicken.send(1);
chicken.send(0);
chicken.send(2);
chicken.send(3);
chicken.send(1);
chicken.send(0,2);
chicken.send(1,2);
chicken.qtime();
system("pause");
return 0;
}