// stringctortest.cpp : 定义控制台应用程序的入口点。
//测试string类的六个构造方法
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string one("my first string test");
cout<<one<<endl;
string two(20,'@');
cout<<two<<endl;
string three(one,3);
cout<<three<<endl;
string four;
four=one+three;
cout<<four<<endl;
char a[]="It's a long way";
string five(a,10);
cout<<five<<endl;
string six(&one[0],&one[10]);
cout<<six<<endl;
return 0;
}
//测试string类的六个构造方法
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string one("my first string test");
cout<<one<<endl;
string two(20,'@');
cout<<two<<endl;
string three(one,3);
cout<<three<<endl;
string four;
four=one+three;
cout<<four<<endl;
char a[]="It's a long way";
string five(a,10);
cout<<five<<endl;
string six(&one[0],&one[10]);
cout<<six<<endl;
return 0;
}
上例中一共使用string类的六种构造函数。
第一个是赋给string对象one一个字符串。
第二个是赋给string对象two20个@符号。
第三个是将one从第三个位置开始到最后的字符赋给three。
第四个是创建一个没有值的默认的string对象。
第五个是将a指向的前10个字符赋给five。
第六个是将one的第一个至第十个字符赋给six。
其中第六个构造函数原型为:
template<class Iter>
string(Iter begin,Iter end)
即将string对象初始化为区间[begin,end]内的字符,其中begin和end的行为就像指针,用于指定位置,范围包括begin在内,但不包括end。