struct初始化
struct studentInfo
{
studentInfo():num(123)
{
name = "lzc";
}
std::string name;
int num;
};
int main()
{
studentInfo info;
std::cout << info.name <<info.num<< std::endl;
return 0;
}
struct可以直接使用=号,或是默认拷贝构造函数赋值
#include "stdafx.h"
#include <stdio.h>
#include <string>
#include <iostream>
struct studentInfo
{
std::string name;
int num;
};
int main()
{
studentInfo info;
info.name = "lzc";
info.num = 5;
//studentInfo info2(info);
studentInfo info2 = info;
std::cout << info2.name << " " << info2.num << std::endl;
return 0;
}
struct字符数组的赋值:
#include "stdafx.h"
#include <stdio.h>
#include <string>
#include <iostream>
#include <vector>
struct studentInfo
{
char buf[128];
int num;
};
int main()
{
studentInfo info;
int nSize = sizeof(studentInfo);
std::cout << "sizeof(studentInfo)=" << nSize << std::endl;
char* sz = "Hello, World\n";
strcpy_s(info.buf, sz);
info.num = 123;
std::cout << info.buf << info.num << std::endl;
return 0;
}
struct中char*的赋值
#include "stdafx.h"
#include <stdio.h>
#include <string>
#include <iostream>
#include <vector>
struct studentInfo
{
char *pSz;
int num;
};
int main()
{
studentInfo info;
int nSize = sizeof(studentInfo);
std::cout << "sizeof(studentInfo)=" << nSize << std::endl;
char* sz = "Hello, World\n";
info.pSz = sz;
info.num = 123;
std::cout << info.pSz << info.num << std::endl;
studentInfo info2 = info;
std::cout << info2.pSz << info2.num << std::endl;
return 0;
}
struct里的char*如果是new出来的,则在该变量delete之后,调用就会出错,这就是赋值的浅copy,如果要深copy则要将该指针变量也new一遍,并且给这个内存区域赋值,
#include "stdafx.h"
#include <stdio.h>
#include <string>
#include <iostream>
#include <vector>
struct studentInfo
{
char *pSz;
int num;
};
int main()
{
studentInfo info;
int nSize = sizeof(studentInfo);
std::cout << "sizeof(studentInfo)=" << nSize << std::endl;
//char* sz = "Hello, World\n";
char* sz = new char[128];
sz[0] = 'H';
sz[1] = 'e';
sz[2] = 'l';
sz[3] = 'l';
sz[4] = 'o';
sz[5] = 0;
info.pSz = sz;
info.num = 123;
std::cout << info.pSz << info.num << std::endl;
delete []sz;
studentInfo info2 = info;
std::cout << info2.pSz << info2.num << std::endl;
return 0;
}
struct里面vector等变量可直接赋值
// ConsoleApplication10.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <string>
#include <iostream>
#include <vector>
struct studentInfo
{
std::vector<std::string> names;
int num;
};
void max(int a, int b)
{
a = 1;
b = 2;
}
void fn(int& a, int& b)
{
a = 1;
b = 2;
}
void test(int* a, int* b)
{
*a = 8;
*b = 9;
}
int main()
{
studentInfo info;
std::vector<std::string> nameList;
nameList.push_back("qwe");
nameList.push_back("980");
info.names = nameList;
info.num = nameList.size();
std::cout << info.num << std::endl;
for (int i = 0; i < info.num; i++)
{
std::cout << info.names[i] << std::endl;
}
studentInfo info2(info);
std::cout << info2.num << std::endl;
for (int i = 0; i < info2.num; i++)
{
std::cout << info2.names[i] << std::endl;
}
int num1 = 100;
int num2 = 200;
max(num1, num2);
std::cout << num1 <<","<<num2<< std::endl;
fn(num1,num2);
std::cout << num1 << "," << num2 << std::endl;
test(&num1, &num2);
std::cout << num1 << "," << num2 << std::endl;
return 0;
}