第2章 对象的创建与使用(二)

本文介绍了C++编程的基础知识,包括程序结构、输入输出流、字符串处理、文件读写及vector容器的使用等核心概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2.2.2 连接

    连接器把由编译器生成目标模块连接成为操作系统可以加载和执行的程序。

2.2.3.使用库文件

使用库必须:

1)包含库头文件。

2)使用库中的函数和变量。

3)把库连接进可执行文件。

2.2.3.1 连接器如何查找库

CC++要对函数和变量进行外部引用时,根据引用情况,连接器会选择两种处理方法中一种。如果还未遇到过这个函数或变量的定义,连接器会把它的标识符加到“未解析的引用”列表中。如果连接器遇到过函数或变量的定义,那么着就是已解决的引用。

如果连接器在目标块中不能找到函数或变量的定义,它将去查找库。

注意:仅仅是库总包含所需定义的目标模块加入连接,而不是整个库参加连接。

2.2.3.2 秘密的附加模块

    当创建一个C/C++可执行程序时,连接器会秘密链接某些模块。其中之一是启动模块,它包含了对程序的初始化例程。初始化例程是开始之执行C/C++程序时必须首先执行一段程序。初始化例程建立堆栈,并初始化程序中的某些变量

 

2.3 编写第一个C++程序

2.3.1 使用iostream

为了声明iostream类中的函数和外部数据,要用如下语句包含头文件:

#include <iostream>

标准输出的含义就是“发送输出的通用场所”。

将数据发送标准输出,要用操作符“<<”。

Cout << “howdy!”;

意思就是把字符串“howdy!”发送到cout对象。

2.3.2 名字空间

namespace关键字。库或程序中的每一个C++定义集被封装在一个名字空间中,如果其他的定义中有相同的名字,但他们在不同名字空间,就不会产生冲突。

而需要使用名字空间,则需要使用关键字using

using namespace std;

#include <iostream.h>

相当于

#include <iostream>

using namespace std;

2.3.3 程序的基本结构

C/C++程序是变量、函数定义、函数调用的集合。

int function()

{

     // Function code here

}

注意C++语言中,main()函数总是返回int类型。

注释有两种:

1)    以“/*”开始,以“*/”结束。

2)    使用“//”(适合单行注释)。

2.3.4 Hello, World!

//:c02:Hello.cpp
// Saying Hello with C++
#include <iostream>
using namespace std;

int main()
{
	cout << "Hello, World! I am "
		 << 8 << " Today!" << endl; 
} ///:~


2.3.5 运行编译器

2.4 关于输入输出流

//: C02:Stream2.cpp
// More streams features
#include <iostream>
using namespace std;

int main()
{
	// Specifying formats with manipulators
	cout << "a number in decimal: "
		 << dec << 15 << endl;
	cout << "in octal: " << oct << 15 << endl;
	cout << "in hex: " << hex << 15 << endl;
	cout << "a floating-point number: "
		 << 3.14159 << endl;
	cout << "non-printing char (escape): "
		 << char(27) << endl;
} ///:~


2.4.1 字符串组的拼接

    C预处理器的一个重要功能就是可以进行字符数组的拼接。

//: C02:Concat.cpp
// character array Concatenation
#include <iostream>
using namespace std;

int main()
{
	cout << "This is far too long to put in a "
		 "single line but it can be broken up with "
		 "no ill effects\nas long as there is no "
		 "punctuation separating adjacent character "
		 "arrays.\n";
} ///:~


2.4.2 读取输入数据

用来完成标准输入功能的对象是cinCin通常是指从控制台输入,但这种输入可以重定向来自其他输入源。

cin一起使用的输入输出流操作符是>>

//: C02:Numconv.cpp
// Converts decimal to octal and hex
#include <iostream>
using namespace std;

int main()
{
	int number;
	cout << "Enter a decimal number: ";
	cin >> number;
	cout << "Value in octal = 0"
		 << oct << number << endl;
	cout << "Value in hex = 0x"
	     << hex << number << endl;
} ///:~


2.4.3 调用其他程序

//: C02:CallHello.cpp
// Call another program
#include <cstdlib>

int main()
{
	system("pause");
} ///:~


2.5 字符串简介

//:C02:HelloStrings.cpp
// The basics of the Standard C++ string class
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s1, s2;
	string s3 = "Hello, World.";
	string s4("I am");
	s2 = "Today";
	s1 = s3 + " " + s4;
	s1 += " 8 ";
	cout << s1 + s2 + "!" << endl;
} ///:~


    可以用“=”来给string对象赋值;连接string对象,只需要用“+”操作符。

2.6 文件的读写

为了打开文件进行读写操作,必须包含<fstream>;为了读而打开文件,要创建一个ifstream对象,用法与cin相同,为了写而打开文件,要创建一个ofstream对象,用法与cout相同。

函数getline(),用它可以把一行读入到string对象中。getline()第一个参数是ifstream对象,从中读取内容,第二个参数是stream对象

//:C02:Scopy.cpp
// Copy one file to another, a line at a time
#include <fstream>
#include <string>
using namespace std;

int main()
{
	ifstream in("Scopy.cpp");
	ofstream out("Scopy2.cpp");
	string s;
	while (getline(in, s))
	{
		out << s << "\n";
	}
} ///:~


    也可以把整个文件拷贝给一个string对象,例如:

//:C02:FillString.cpp
// Read an entire file into a single string
#include <fstream>
#include <string>
#include <iostream>
using namespce std;

int main()
{
	ifstream in("FillString.cpp");
	string s, line;
	while (getline(in, line))
	{
		s += line + "\n";
	}
	
	cout << s << endl;
} ///:~


 

2.7 vector简介

//:C02:FillVector.cpp
// Copy an entire file into a vector of string
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
	ifstream in("FillVector.cpp");
	string s;
	vector<string> v;
	while (getline(in, s))
	{
		v.push_back(s);
	}
	
	for (int i = 0; i < v.size(); i++)
	{
		cout << i << ":" << v[i] << endl;
	}
	return 0;
} ///:~


 

读取单词:

//:C02:GetWords.cpp
// Break a file into whitespace-separated words
#include <fstream>
#include <vector>
#include <iostream>
#include <string>
using namespace std;

int main()
{
	vector<string> words;
	ifstream in("GetWords.cpp");
	string word;
	while (in >> word)
	{
		words.push_back(word);
	}
	
	for (int i = 0; i < words.size(); i++)
	{
		cout << words[i] << endl;
	}
	return 0;
}///:~


while(in >> word)

意思是每次取输入的一个单词。

下面一个列子是vector存储int类型例子: 

//:C02:IntVector.cpp
// Creating a vector that holds integers
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	
	for (int i = 0; i < v.size(); i++)
	{
		cout << v.[i] << endl;
	}
	
	for (int i = 0; i < v.size(); i++)
	{
		v[i] = v[i] * 10;
	}
	
	for (int i = 0; i < v.size(); i++)
	{
		cout << v{i} << endl;
	}
}///:~


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凡唯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值