做网站用vs,成都旅游必去景点,网络运营好学吗,效果图网址结构体
c中定义结构体变量#xff0c;可以省略struct关键字
c结构提重可以直接定义函数#xff0c;谓之成员函数#xff08;方法#xff09;
#include iostream
using namespace std;
struct stu {int num;char name[24];void price(void) {cout 中定义结构体变量可以省略struct关键字
c结构提重可以直接定义函数谓之成员函数方法
#include iostream
using namespace std;
struct stu {int num;char name[24];void price(void) {cout this is name endl;}
};int main() {stu st;cin st.num st.name;st.price();return 0;
}联合
c中定义联合体变量可以省略union关键字支持匿名联合
#include iostream using namespace std;
int main(void) { union { //匿名联合相当于局部变量 int num; char c[4]; }; num 0x12345678; cout hex (int)c[0] (int)c[1] (int)c[2] c[3] endl; }
#include iostream
using namespace std;int main(void) {union { //匿名联合相当于局部变量int num;char c[4];};num 0x12345678;cout hex (int)c[0] (int)c[1] (int)c[2] c[3] endl;
}枚举类型
C中定义枚举变量可以省略enum关键字
C中枚举是独立的数据类型不能当做整型数使用
#include iostream
using namespace std;int main() {enum COLOR {RED,GREEN,BULE};COLOR C RED;cout C endl;return 0;
}
//输出值为0C不能直接等于整数
布尔类型
C中布尔bool是基本数据类型专门表示逻辑值
布尔类型的字面值常量
true表示逻辑真
false表示逻辑假
布尔类型本质单字节的整数使用1表示真0表示假
任何基本类型都可以被隐式转换成布尔类型
#include iostream
using namespace std;int main(int argc, const char *argv[])
{bool c;c false;cout c endl;cout boolalpha c endl;c 2 3;cout boolalpha c endl;return 0;
}//输出结果为0false,true
C对字符串的处理
C专门设计了string类型表示字符串
string类型字符串定义
string s;//定义空字符串
//想字符串插入内容的三种方式
string s(hello);
string s hello
string s string(hello);
字符串拷贝
string s1 hello
string s2 s1;字符串连接
string s1 hello, s2 world;
string s3 s1 s2;//s3:hello world
s1s2;//s1:hello world;
字符串比较
string s1 hello,s2 world;
if (s1 s2) cout s1 s2 endl;
else cout s1 endl;
随机访问
//支持下标访问
string s hello;
s[0] H;
获取字符串长度
size_t size();
size_t length();
转换为C风格的字符串
const char *c_str();
字符串交换
void swap(string s1,string s2);