C APIs for string C++ APIs for string
best APi for delimiter:
1. istringstream, 空格会成为字符串参数的内部分界。
istringstream是由一个string对象构造而来,istringstream类从一个string对象读取字符。
istringstream的构造函数原形如下:istringstream::istringstream(string str);
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
istringstream istr;
istr.str("1 56.7");
//上述两个过程可以简单写成 istringstream istr("1 56.7");
cout << istr.str() << endl;
int a;
float b;
istr >> a;
cout << a << endl;
istr >> b;
cout << b << endl;
return 0;
}
构造字符串流的时候,空格会成为字符串参数的内部分界
bob@bob:~/workplace$ ./a
1 56.71
56.7
online test:
2 hours for C++ string's add function with 9 requirements.
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include "string.h"
#include<cctype>
using namespace std;
//0--not digital
//1--is digital
//2--negative number
int isDig(const char* a, int n){
int i=0;
int result = 0;
for(;i<n;i++){
if(( a[i]>'9' || a[i]<'0')&& a[i] != '-')
return 0;
if(i > 0 && a[0] == '-')
result = 2;
}
if( result ==2 )
return result;
else
return 1;
}
int add(string str){
vector<int> nums;
vector<int> negnums;
// vector<char*> nums;
int sum = 0;
// char * cstr = new char [str.length()+1];
// strcpy (cstr, str.c_str());
//
// // cstr now contains a c-string copy of str
char* input = new char[str.length()+1];
strcpy(input, str.c_str());
char* r = strtok(input,",//n//;*%[]");
cout<<"split: "<<r<<" ";
while(r != NULL){
int digresult = isDig(r,strlen(r));
if( digresult == 1 && stoi(r) <1000 )
{
nums.push_back( stoi(r));
cout<<"push"<<endl;
}else if( digresult ==2 )
{
negnums.push_back(stoi(r));
cout<<"push - "<<endl;
}
r = strtok(NULL,",//n//;*%[]");
if(r != NULL)
cout<<r<<" ";
}
if( !negnums.empty()){
stringstream ss;
cout<<"Negative number exception:"<<endl;
ss<<"Negative number exception:";
while( !negnums.empty())
{
ss<<negnums.back()<<",";
negnums.pop_back();
}
throw ss.str();
}
while( !nums.empty() ){
sum += nums.back();
nums.pop_back();
}
return sum;
}
int main(){
cout<<"input q for quit"<<endl;
string input;
int result = -1;
getline(cin,input);
cout<<"first input:"<<input<<endl;
while( input.compare("q") )
{
try{
result = add( input);
cout<<"Summation:"<<result<<endl;
}catch(string e){
cout<<e;
}
getline(cin,input);
}
return 1;
}
Practice a 360 programming input test question
#include<iostream>
#include<sstream>
using namespace std;
int main()
{
int numOfmine = 0;
int numOfarray = 0;
cin>>numOfarray;
int *mine[numOfarray] ={0};
if( numOfarray > 0 ){
for (int j = 0 ; j < numOfarray ; j++ ){
cout<<"j="<<j<<endl;
cin>>numOfmine;
cout<<"input :";
cin.ignore();
string str;
istringstream in;
getline(cin,str);
cout<<"type:"<<str<<endl;
in.str(str);
if ( numOfmine > 0 )
{
mine[j] = new int[numOfmine];
int index = 0;
while(index < numOfmine){
in>>mine[j][index++];
cout<<"value:"<<mine[j][index-1]<<endl;
}
}
}
}
}
搜狐-input testing in c++
#include <iostream>
#include<sstream>
#include<string>
//#define __STDC_WANT_LIB_EXT1__ 1
//#include <stdio.h>
#include <cstdio>
using namespace std;
int main(){
int n,k = 0;
int rowN;
for(int i = 0; i < 2; i++){
// cin.ignore();
string s;
if( rowN < 0 ){
continue;
}
if( i == 0 ){
getline(cin,s);
stringstream ss(s);
for( int i = 0 ; i < 2 ; i++)
ss>>n>>k;
cout<<n<<","<<k<<endl;
}
if( i == 1 ){
if(n < 0 || k < 0 )
break;
int rowData[n];
int minData[k];
string str;
// cin.ignore();
getline(cin, str);
cout<<str<<endl;
stringstream ss(str);
for( int i = 0 ; i < n ; i++)
{ss>>rowData[i];
cout<< rowData[i]<<",";
}
cout<<"k n="<<k<<n<<endl;
for( int x = 0 ; x < k ; x++ ){
for( int y = x ; y < n ; y++ ){
if( rowData [x] >= rowData[y] ){
int tem=0;
tem = rowData[x];
rowData[x] = rowData[y];
rowData[y] = tem;
}
}
}
cout<<rowData[0]<<","<<rowData[1]<<","<<rowData[2]<<","<<rowData[3]<<",";
cout<< endl;
}
}
return 0;
}