c++学习--C/C++常用函数
来源:互联网 发布:apache重启不了 编辑:程序博客网 时间:2024/06/10 04:42
Strstr
strncpy
strcpy_s //VC6.0没有此函数
strcpy
CString 转换 string
CString 转换 char数组
String 转换 double
Char * 转换 string
Int 转换string
String 转换char[]
atol
Convert string to long integer (function)
strtoul
Convert string to unsigned long integer (function)
strtod
Convert string to double (function)
atoi
Convert string to integer (function)
atof
Convert string to double (function)
strtol
Convert string to long integer (function)
/***************** strstr example ******************/
#include <iostream>
using namespace std;
int main ()
{
char str[] ="This is a imple string";
char * pch;
pch = strstr (str,"s"); //pch没有开辟新的地址,还是指向str的地址空间,次例输出可以说明问题
strncpy (pch,"sample",6);
puts (str);
strcpy(st,pch);
cout<<*pch<<endl;//此输出只有一个字符,若想要全部还需要一个缓冲区
char a[2];
char* b;
a[0]='[';//不能为a[0]="w"; 此为两个字符,还有一个'/0'
a[1]=50;
b=strstr("@@![#$",a);
return 0;
}
/**********************strncpy example ***************************/
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]= "To be or not to be";
char str2[6];
strncpy (str2,str1,5);//将str1前五个字符复值给str2
str2[5]='/0';
puts (str2);
return 0;
}
/************************strcpy_s(VC6.0没有此函数)和strcpy()****************************/
strcpy_s和strcpy()函数的功能应该一样的。strcpy函数,就象gets函数一样,它没有方法来保证有效的缓冲区尺寸,所以它只能假定缓冲足够大来容纳要拷贝的字符串。在程序运行时,这将导致不可预料的行为。用strcpy_s就可以避免这些不可预料的行为。
这个函数用两个参数、三个参数都可以,只要可以保证缓冲区大小。
三个参数时:
errno_t strcpy_s(
char *strDestination,
size_t numberOfElements,
const char *strSource
);
两个参数时:
errno_t strcpy_s(
char (&strDestination)[size],
const char *strSource
); // C++ only
#include<iostream>
#include<string>
using namespace std;
void main( )
{
char *str1=NULL;
str1=new char[20];
char str[7];
strcpy_s(str1,20,"hello world");//三个参数
strcpy_s(str,"hello");//两个参数但如果:char *str=new char[7];会出错:提示不支持两个参数
cout<<"strlen(str1)"<<strlen(str1)<<"strlen(str)"<<strlen(str)<<endl;
printf(str1);printf("/n");
cout<<str<<endl;
}
1.CString 转换 string
CString str;
GetDlgItem(IDC_EDIT1)->GetWindowText(str);
info.account = str.GetBuffer(0);
str.ReleaseBuffer();
2.CString 转换 char数组
char *p;
char c[10];
p=str.GetBuffer(str.GetLength());
str.ReleaseBuffer();
strcpy(c,p);
3.string to double
10 #include <iostream>
11 #include <string>
12 #include <cstdlib>
13
14 using namespace std;
15
16 int main() {
17 string s = "123";
18 double n = atof(s.c_str());
19 //int n = atoi(s.c_str());
20
21 cout << n << endl;
22 }
4.Char * 转换 string
char* cExpression
string str(cExpression);
5.int转string
string转char[]
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
stringstream sstr;
//--------int转string-----------
int a=100;
string str;
sstr<<a;
sstr>>str;
cout<<str<<endl;
//--------string转char[]--------
sstr.clear();//如果你想通过使用同一stringstream对象实现多种类型的转换,请注意在每一次转换之后都必须调用clear()成员函数。
string name = "colinguan";
char cname[200];
sstr<<name;
sstr>>cname;
cout<<cname;
system("pause");
}
http://blog.csdn.net/liufei_learning/article/details/6290278
- C学习小结【常用函数】
- c++学习--C/C++常用函数
- C#.net常用函数
- c#.net常用函数
- c 常用函数说明
- Linux 常用C函数
- C常用函数
- C/C++常用函数
- linux 常用c函数
- C 常用函数解析
- C 常用字符串函数
- C# 常用字符串函数
- c常用字符串函数
- c常用函数实现
- 常用c 函数
- C 常用函数集
- C语言常用函数
- LINUX常用C函数
- 郁闷 。。。
- C++学习--虚函数表解析 .
- c++学习--memset用法详解
- jQuery中添加自定义或函数方法
- ExtJS 3.1.0中Ext.data.Store关于baseParams的BUG修正
- c++学习--C/C++常用函数
- WS_EX_TOOLWINDOW 属性的陷阱
- TI OMAP35X系统为什么需要x-load
- 动态表格,通过按钮增加行,删除时删除选择的checkbox那一行
- Domino错误信息的详细分析
- Linux下的多线程编程
- 配置 SQL SERVER2008 数据库邮件 功能
- 在python 中调用Dll
- 怎么退出整个框架