---
title: "C＋＋基础知识笔记(1)"
author: "chrisongs"
date: "2010-12-21T17:04:26Z"
tags: ["C", "C#", "C++", "F#", "J#"]
categories: ["编程语言"]
slug: "c-1"
draft: false
lastmod: "2010-12-21T17:04:26Z"
id: 848437
url: "https://chrisongs.iteye.com/blog/848437"
---

1\. sizeof操作符 与 strlen()函数的区别: \[code="c++"]#include #include using namespace std; int main() { int p1\[5] = {1,2,3,4,5}; char p2\[20] = "hello world!"; cout &lt;&lt; sizeof(p1) &lt;&lt; endl; cout &lt;&lt; sizeof(p2) &lt;&lt; '\\t' &lt;&lt; strlen(p2) &lt;&lt; endl; return 0; }\[/code] 输出为:20 20 12 总结:(1)sizeof操作符指出整个数组的长度,但strlen()函数返回的是存储在数组中的字符串的长度。 (2)strlen()函数只能用于存储字符串的数组(即char数组),否则编译器会报错 2. string类与char\[]数组的区别: \[code="java"]#include #include #include using namespace std; int main() { char p1\[30] = "hello world!"; string s1 = "hello world!"; cout &lt;&lt; strlen(p1) &lt;&lt; endl; cout &lt;&lt; s1.size() &lt;&lt; endl; return 0; }\[/code] 输出为:12 12 总结:函数size()的功能与strlen()基本相同,但句法不同:s1不是作为函数参数,而是位于函数名之前,它们之间用句点连接.表明,s1是一个对象,而size()是一个类方法. 3. C++中的结构简介: \[code="c++"]#include #include #include using namespace std; struct Test { private: int i; string s ; public: void setIValue(int i); int getIValue(); void setSValue(string s); string getSValue(); void HW(); }; void Test::setIValue(int i) { Test::i = i; } int Test::getIValue() { return Test::i; } void Test::setSValue(string s) { Test::s = s; } string Test::getSValue() { return Test::s; } void Test::HW() { for (int j=0; j #include #include using namespace std; struct Test { string name; int age; char sex; void hello(); }; void Test::hello() { cout &lt;&lt; "hello " &lt;&lt; name &lt;&lt; endl; } int main() { /* Test test = { "chris", 24, 'm' }; cout &lt;&lt; test.name &lt;&lt; endl &lt;&lt; test.age &lt;&lt; endl &lt;&lt; test.sex &lt;&lt; endl; \*/ Test test1\[2] = { {"chris", 24, 'm'}, {"harry", 23, 'f'} }; for(int i=0; i&lt;2; i++) { cout &lt;&lt; test1\[i].name &lt;&lt; endl &lt;&lt; test1\[i].age &lt;&lt; endl &lt;&lt; test1\[i].sex &lt;&lt; endl; test1\[i].hello(); } return 0; }\[/code] 输出为: chris 24 m hello chris harry 23 f hello harry 总结:可像以上代码一样初始化结构中的变量. 4. 枚举： \[code="c++"]#include #include #include using namespace std; int main() { enum color {red, blue, green, yellow}; cout &lt;&lt; red &lt;&lt; '\\t' &lt;&lt; blue &lt;&lt; '\\t' &lt;&lt; green &lt;&lt; '\\t' &lt;&lt; yellow &lt;&lt; endl; enum number {first, second=100, third=125, fourth=125, fifth, sixth}; cout &lt;&lt; first &lt;&lt; '\\t' &lt;&lt; second &lt;&lt; '\\t' &lt;&lt; third &lt;&lt; '\\t' &lt;&lt; fourth &lt;&lt; '\\t' &lt;&lt; fifth &lt;&lt; '\\t' &lt;&lt; sixth &lt;&lt; endl; color myFlag1,myFlag2; myFlag1 = color(2); cout &lt;&lt; myFlag1 &lt;&lt; endl; myFlag2 = color(5); cout &lt;&lt; myFlag2 &lt;&lt; endl; return 0; }\[/code] 输出为： 0 1 2 3 0 100 125 125 126 127 2 5 总结：熟悉枚举的使用规则. 5 指针和数字: \[code="c++"]#include #include #include using namespace std; int main() { int num = 5; int \*p1, \*p2; p1 = # p2 = new int; \*p2 = 6; cout &lt;&lt; \*p1 &lt;&lt; '\\t' &lt;&lt; p1 &lt;&lt; endl; cout &lt;&lt; \*p2 &lt;&lt; '\\t' &lt;&lt; p2 &lt;&lt; endl; delete p2; cout &lt;&lt; \*p2 &lt;&lt; '\\t' &lt;&lt; p2 &lt;&lt; endl; return 0; }\[/code] 输出为: 5 0xbfc91268 6 0x9fc5008 0 0x9fc5008 6. 使用new来创建动态数组: 在编译时给数组分配内存被称为静态联编,意味着数组是在编译时加入到程序中的;但使用new时,如果在运行阶段需要数组,则创建它;如果不需要,则不创建.还可以在程序运行时选择数组的长度.这被称为动态联编,意味着数组是在程序运行时创建的.这种数组叫做动态数组.使用静态联编时,必须在编写程序时指定数组的长度;使用动态联编时,程序将在运行时确定数组的长度. \[code="c++"]#include #include #include using namespace std; int main() { int* pt = new int\[5]; for(int i=0; i&lt;5; i++) { pt\[i] = i; } for(int j=0; j&lt;5; j++) { cout &lt;&lt; \*(pt+j) &lt;&lt; endl; } delete \[] pt; return 0; }\[/code] 输出为: 0 1 2 3 4 7. 二维数组: \[code="c++"]#include #include #include using namespace std; int main() { int numArray\[3]\[4] = {{1,2,3,4},{2,3,4,5},{3,4,5,6}}; for(int i=0; i&lt;3; i++) { for(int j=0; j&lt;4; j++) { cout &lt;&lt; numArray\[i]\[j] &lt;&lt; '\\t'; } cout &lt;&lt; endl; } return 0; }\[/code] 输出为: 1 2 3 4 2 3 4 5 3 4 5 6