#include<iostream> #include <vector> using namespace std; int main() { unsigned int off,len;//子串开始位置,长度 vector<char>v;//声明容器 char temp; //确保空格也能读进去 while (cin.get(temp)&&temp!='\n') { v.push_back(temp);//添加元素 } cout<<v.size()<<endl; cout<<"Please input the starting position and the length of substring\n"; cin.clear();//恢复流状态 cin>>off>>len; if((off+len)-1>v.size())//判断求子串的输入是否有误 { cout<<"Wrong Input!"<<endl; return 0; } for (vector<char>::iterator iter=v.begin()+off-1;iter!=v.begin()+off+len-1;iter++)//输出子串 { cout<<*iter; } cout<<endl; for(int i=off-1;i<off+len-1;i++) { cout<<v[i]; } cout<<endl; return 1; } //*********wuxinliulei于2010/7/7*******************************
发现了 如下问题:向量vector的变量cvec的函数值cvecc.size()是unsigned的类型
本程序首先输入字符串直至输入回车字符
比如输入123456789
然后提示输入beg 与 len
比如输入 3 5
则输出34567 从3开始的5个字符
cvec .begn() 其实是从1开始的,比如cvec.begin()+len 就代表已经到达了数组的len+1的位置而不是第len个字符
但是在vector序列中其实起始位置还是从0开始的,就是从push_back
进去的位置是0的
string类的使用
#include <string> // 使用 string 类时须包含这个文件
#include <iostream>
#include<cstdio>
using namespace std;
int main()
{
string str1;
// 输入与输出
cout << "输入字符串 str1: (cin>> 遇到空格就停止了)" << endl;
cin >> str1;
getchar();
cout << str1 << endl;
// 一行行读取
cout << "输入字符串 str1: (getline(cin,stringName)是逐行读取的)" << endl;
getline( cin, str1 );
cout << str1 << endl;
// 与 c字符转换
string str2("Hello World!"), str3;
char str4[50];
cout << "输入 C 字符串" << endl;
scanf("%s",str4);
str3= str4; //string类型可以直接赋值 C语言字符串当然不行的
cout << "str2 is " << str2 << endl;
cout << "str3 is " << str3 << endl;
// 求字符串的长度
string str5;
cout << "输入字符串 str5" << endl;
cin >> str5;
int len= str5.size();
cout << "字符串 str5的长度为" << len << endl << endl << endl;
// 遍历字符串例子
string str6;
cout << "输入字符串 str6" << endl;
cin >> str6;
unsigned i;
for( i= 0; i< str6.size(); ++i )
cout << str6[i]<<" ";
cout << endl << endl;
// 比较两个字符串 比较规则同 c字符串比较规则
string str7, str8;
cout << "输入字符串 str7, str8 , 中间用空格格开" << endl;
cin >> str7 >> str8;
if( str7< str8 ) cout << str7 << " 小于 " << str8 << endl;
else if( str7> str8 ) cout << str7 << " 大于 " << str8 << endl;
else cout << str7 << " 等于 " << str8 << endl;
// 字符串与字符相加
string str9= "Darren";
char ch1= 'a', ch2= 'b';
str9= str9+ ch1; cout << str9 << endl;
str9= ch2+ str9; cout << str9 << endl;
// 字符串与字符串相加
string str10= "Acm", str11= "ICPC";
str10.append( str11 );
cout << str10 << endl;
// 字符串是否包含子串 如果包含 则返回子串在目标串中第一次出现的位置
string str12= "I am a student", str13= "student", str14= "aaaaaaa";
if( (int)str12.find( str13 )!= -1 ) cout << "Find " << str13 << endl;
if( (int)str12.find( str14 )== -1 ) cout << "Not Find " << str14 << endl;
// 转换成 c_字符串
string str15= "Hello World";
printf("%s\n", str15.c_str() );
//求取子串
string str16="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
string str17 = str16.substr (12,12); // "generalities"
unsigned pos = str16.find("live"); // position of "live" in str
printf("%d\n",pos);
string str18 = str16.substr (pos); // get from "live" to the end
cout << str17 << ' ' << str18<< '\n';
return 0;
}
#include<cstdio>
int main()
{
int start,end,i,j;
char s[100];
gets(s);
char *p=s;
printf("请输入你想要的子串范围(0开始,开始位置,结束位置):\n");
scanf("%d%d",&start,&end);
i = start;
while(i<=end)
{
putchar(*(p+i));
i++;
}
printf("\n");
char *ans;
i = start; j = 0;
while(i<=end)
{
*(ans+j) = *(p+i);
i++;j++;
}
*(ans+j)='\0';
printf("%s",ans);
return 0;
}