一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

c++ 字符串输出操作

时间:2011-06-01 编辑:简简单单 来源:一聚教程网

c++ 字符串输出操作

#include
#include
#include
using namespace std;

int main()
{
   string words[5] = { "ABCD", "BCDEF", "CERF","DERT", "EFRE"};
   string*   where;

   where = find(words, words + 5, "CD");
  
   cout << *++where << endl;               
  
   sort(words, words + 5);
  
   where = find(words, words + 5, "ER");
   cout << *++where << endl;             
}
 
实例二

#include
#include  

using namespace std;

int main()
{
   string string1;
   string string2;

   cout << "nEnter a string: ";
   getline( cin, string1 );

   cout << "Enter a second string: ";
   getline( cin, string2 );

   cout << "nFirst string: " << string1 << endl;
   cout << "Second string: " << string2 << endl;

   cout << endl;

   return 0;
}

复制一个函数

#include
#include
#include
#include
#include

using namespace std;

int main( )
{

   string s( "this is a test." );
   cout << s << endl;
   copy( s.begin(), s.end(), ostream_iterator( cout, " " ) );
}

参考

 

char *strcpy(char *s1, const char *s2)
将字符串s2复制到字符串数组s1中,返回s1的值

char *strncpy(char *s1, const char *s2, size_t n) 
将字符串s2中最多n个字符复制到字符串数组s1中,返回s1的值

char *strcat(char *s1, const char *s2)
将字符串s2添加到字符串s1的后面。s2的第一个字符重定义s1的null终止符。返回s1的值

char *strncat(char *s1, const char *s2, size_t n)
将字符串s2中最多n个字符添加到字符串s1的后面。s2的第一个字符重定义s1的null终止符。

返回s1的值

int strcmp(const char *s1, const char *s2)
比较字符串s1和字符串s2。函数在s1等于、小于或大于s2时分别返回0、小于0或者大于0的值

int strncmp(const char *s1, const char *s2, size_t n)
比较字符串s1中的n个字符和字符串s2。函数在s1等于、小于或大于s2时分别返回0、小于0或

者大于0的值

char * strtok(char *s1,const char *s2)
用一系列strtok调用将s1字符串标记化(将字符串分成各个逻辑组件,如同一行文本中的每个

单词),用字符串s2所包含的字符分隔。 首次调用时包含s1为第一个参数,后面调用时继续

标记化同一字符串,包含NULL为第一个参数。每次调用时返回当前标记指针。如果函数调用

时不再有更多标记,则返回NULL

size_t strlen(const char *s)
确定字符串长度,返回null终止符之前的字符数

热门栏目