C/C++风格字符串
一:字符串类型分类:
1. C风格字符串:起源于C,并在C++中得到支持。
a. 存储方式:被存储在一个字符数组中,通过一个char*类型的指针来操纵它。
b. 提供的库函数:包含头文件:#include<cstring>
a) int strlen( const char* );//返回字符串的长度
b) int strcmp( const char*, const char* );//比较两个字符串是否相等
c) char* strcpy( char*, const char* );//把第二个字符串拷贝到第一个字符串中
#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;
void main()
{
char* arraya = "hello world!";
int c;
c = strlen(arraya);
cout<<c<<endl;
char arrayb[80];
char * test = NULL;
test = strcpy (arrayb, arraya);
cout<<"test is: "<<test<<endl;
cout<<arrayb<<endl;
int d;
d = strcmp(arrayb,arraya);
cout<<endl<<"d->"<<d<<endl;
}这段代码包含了以上三个库函数的使用。
二:c++风格的string
1. 我们使用string,它的库函数有很多,当我们需要获得某个字符串的长度的时候,可以调用size()函数:
这里我们定义一个string:string sa = “hello world!”;
1) 我们调用size()函数:sa.size();它的值就是12;
2) 我们需要判断一个字符串是否为空的时候,我们可以调用empty()函数来判断:sa.empty();若这个字符串为空,那么它将返回1,若不为空,则返回0。
3) 我们会用到拷贝字符串的情况,在我们初始化一个字符串的时候,我们可以初始化成一个字符串的拷贝:string sc(sa);在这段代码后,sc的内容和sa就完全相同了。我们可以使用一个判断语句来判断这两个字符串是否相等:if( sc == sa)…
&nb
相关文档:
系统环境:Windows 7
软件环境:Visual C++ 2008 SP1 +SQL Server 2005
本次目的:编写一个航空管理系统
这是数据库课程设计的成果,虽然成绩不佳,但是作为我用VC++ 以来编写的最大程序还是传到网上,以供参考。用VC++ 做数据库设计并不容易,但也不是不可能。以下是我的程序界面,后面 ......
搞软件是有搞头的
——邹祁峰
2009年12月3日
声明:所说的只针对C、C++、.NET专业的同学,对其他专业也许没有参考价值!
[推荐给大三的学弟学妹们]
【欢迎各位朋友补充】
对我个人而言,大学毕业找工作算是画上了一个许多人羡慕,但我自己仍感遗憾的句号。找工作期间 ......
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <math.h>
#include <conio.h>
typedef struct {
short int pitch;
short int duration;
} NOTE;
NOTE notes[] = {{14, 500}, {16, 500}, {12, 500 ......
It is better to have the ability of fast learning
有一天你覺得甚麼都知道了 可悲
有一天你卻發現你甚麼都不會 加油吧
有一天你發現你又甚麼都會了 繼續
有一天你發現你甚麼都不會了 成功了
......
#include <stdlib.h>
#include<stdio.h>
#include<time.h>
#define randomize() srand((unsigned)time(NULL)) //定义一个宏
int main(void)
{
int i;
printf("Ten random numbers from 0 to 99\n\n");
randomize();
for(i=0; i<100; i++)
{
printf("%d\n", rand() % 100);
sleep(1) ......