您的位置:首页 > 其它

感觉经常用到的一些字符处理的函数

2014-07-30 16:04 666 查看


c_str:string.c_str是Borland封装的String类中的一个函数,它返回当前字符串的首字符地址。

c_str函数的返回值是constchar*的,不能直接赋值给char*,所以就需要我们进行相应的操作转化,下面就是这一转化过程。

c++语言提供了两种字符串实现,其中较原始的一种只是字符串的c语言实现。与C语言的其他部分一样,它在c++的所有实现中可用,我们将这种实现提供的字符串对象,归为c-串,每个c-串char*类型的。

标准头文件<cstring>包含操作c-串的函数库。这些库函数表达了我们希望使用的几乎每种字符串操作。 当调用库函数,客户程序提供的是string类型参数,而库函数内部实现用的是c-串,因此需要将string对象,转化为char*对象,而c_str()提供了这样一种方法,它返回const
char*类型(可读不可改)的指向字符数组指针
例:

#include <iostream>

#include <cstring>

using namespace std;

int main()

{

string add_to = "hello!";

const string add_on = "baby";

const char *cfirst = add_to.c_str();

const char *csecond = add_on.c_str();

char *copy = new char[strlen(cfirst) + strlen(csecond) + 1];

strcpy(copy, cfirst);

strcat(copy, csecond);

add_to = copy;

cout << "copy: " << copy << endl;

delete [] copy;

cout << "add_to: " << add_to << endl;

return 0;

}





result

例(1)

函数声明:const char *c_str();

c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同.

这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string
对象转换成c中的字符串样式。

注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针

比如:最好不要这样:

char* c;

string s="1234";

c = s.c_str();

//c最后指向的内容是垃圾,因为s对象被析构,其内容被处理(纠正:s对象的析构是在对指针c完成赋值操作之后进行的,故此处并没有错误)

在vc++2010中提示的错误原因:





vc++2010中提示的错误原因

应该这样用:

char c[20];

string s="1234";

strcpy(c,s.c_str());

这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作

c_str()返回的是一个分配给const char*的地址,其内容已设定为不可变更,如果再把此地址赋给一个可以变更内容的char*变量,就会产生冲突,在2010中是不被允许的。但是如果放入函数调用,或者直接输出,因为这些函数和输出都是把字符串指针作为 const char*引用的,所以不会有问题。

例(2)

c_str() 以const char* 类型返回 string 内含的字符串

如果一个函数要求char*参数,可以使用c_str()方法:

string s = "Hello World!";

printf("%s", s.c_str()); //输出 "Hello World!"

c_str在打开文件时的用处:

当需要打开一个由用户自己输入文件名的文件时,可以这样写:ifstream in(st.c_str());。其中st是string类型,存放的即为用户输入的文件名。

sprintf() 格式化输出函数(图形)

功能: 函数sprintf()用来作格式化的输出。

用法: 此函数调用方式为int sprintf(char *string,char *format,arg_list);

说 明: 函数sprintf()的用法和printf()函数一样,只是sprintf()函数给出第一个参数string(一般为字符数组),然后再调用 outtextxy()函数将串里的字符显示在屏幕上。arg_list为参数表,可有不定个数。通常在绘图方式下输出数字时可调用sprintf()函 数将所要输出的格式送到第一个参数,然后显示输出。

函数名: sprintf

功 能: 送格式化输出到字符串中

用 法: int sprintf(char *string, char *farmat [,argument,...]);

snprintf()是sprintf()的限制字符数量的一个表达

snpirntf(char *string,size_t n,char *format,arg_list); //红色字体的n代表从format这里取前n个字符输入到string中去

atoi:

Convert string to integer
Parses the C-string str interpreting its content as an integral number, which is returned as a value of type
int
.

The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found.
Then, starting from this character, takes an optional initial plus or minus sign followed by as many base-10 digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned.


Parameters

strC-string beginning with the representation of an integral number.


Return Value

On success, the function returns the converted integral number as an
int
value.

If the converted value would be out of the range of representable values by an
int
, it causes undefined behavior. See strtol for
a more robust cross-platform alternative when this is a possibility.


Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14

/* atoi example */
#include <stdio.h>      /* printf, fgets */
#include <stdlib.h>     /* atoi */

int main ()
{
  int i;
  char buffer[256];
  printf ("Enter a number: ");
  fgets (buffer, 256, stdin);
  i = atoi (buffer);
  printf ("The value entered is %d. Its double is %d.\n",i,i*2);
  return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: