字符串转整型

来源于http://topic.csdn.net/t/20051109/12/4381609.html#上的讨论,结果是

  1 #include "stdafx.h"
  2 
  3 
  4 
  5 namespace{
  6     using std::vector;
  7     vector<vector<int> > matrix(6,vector<int>(9)); 
  8 }
  9 
 10 namespace MyTest{
 11 
 12     using std::string;
 13     int StrToInt(const string& srcStr){
 14         string::size_type result=srcStr.find('.');
 15         int bitSize;//位数
 16         int sum=0;
 17         if (result==string::npos){
 18             string::size_type bitSize=srcStr.size();
 19             string::size_type j=bitSize-1;
 20             for(string::size_type i=0;i<bitSize;i++){
 21                 sum+=(srcStr[i]-'0')*
 22                     (static_cast<int>(pow(static_cast<double>(10),static_cast<double>(j--))));
 23             }
 24         }else{
 25             //if (srcStr[result+1]<'5'){
 26             string::size_type j=bitSize=result-1;
 27             for(string::size_type i=0;i<bitSize;i++){
 28                 sum+=(srcStr[i]-'0')*
 29                     (static_cast<int>(pow(static_cast<double>(10),static_cast<double>(j--))));
 30             }
 31                 if (srcStr[result+1]<'5')
 32                     sum+=(srcStr[bitSize]-'0');//四舍
 33                 else
 34                     sum+=((srcStr[bitSize]-'0')+1);//五入
 35             }
 36         return sum;
 37     }
 38 }
 39 
 40 namespace MyTest{
 41     int StrToIntGood(char* str){
 42         int sum=0;
 43         while(*str!='\0'){
 44             sum=sum*10+static_cast<int>(*str-'0');
 45             str++;
 46         }
 47 
 48         return sum;
 49     }
 50 }
 51 
 52 namespace MyTest{
 53     using std::string;
 54     using std::vector;
 55     //typedef vector<int>::size_type s_t; 
 56     int StrToBetter( const string& srcStr){
 57         
 58         //开辟动态数组作为索引
 59         size_t stringSize=srcStr.size();
 60         //size_t row=stringSize-1;
 61         //size_t col=9;
 62         //size_t t=1;
 63         /*
 64         vector<vector<int> > matrix(row,vector<int>(col)); 
 65         for (size_t i=0;i<row;++i){
 66             t=t*10;
 67             for (size_t j=0;j<col;j++){
 68                 matrix[i][j]=t*(j+1);
 69             }
 70         }
 71         */
 72         int sum=0;
 73         for (size_t i=0;i<stringSize-1;++i){
 74             size_t number=srcStr[i]-'0';
 75             sum+=::matrix[stringSize-2-i][number-1];
 76             
 77         }
 78         sum+=srcStr[stringSize-1]-'0';
 79         return sum;
 80     }
 81 }
 82 
 83 using MyTest::StrToInt;
 84 using MyTest::StrToIntGood;
 85 using MyTest::StrToBetter;
 86 using std::string;
 87 using std::cout;
 88 using std::endl;
 89 
 90 int _tmain(int argc, _TCHAR* argv[])
 91 {
 92     
 93     clock_t now;
 94     {
 95         int n;
 96         now=clock();
 97         for(size_t i=0;i<100000;i++)
 98             n=atoi("124565");
 99         now=clock()-now;
100         cout<<"atoi"<<" "<<n+1<<"time:"<<now<<endl;
101     }
102     {
103         
104         int n;
105         now=clock();
106         for(size_t i=0;i<100000;i++)
107             n=StrToInt("124565");
108         now=clock()-now;
109         cout<<"StrToInt"<<" "<<n+1<<"time:"<<now<<endl;
110         //cout<<n+1<<endl;
111         //n=StrToInt("1245.23");
112         //cout<<n+1<<endl;
113         //n=StrToInt("1245.63");
114         //cout<<n+1<<endl;
115     }
116     {
117         char*str=const_cast<char*>("124565");
118         now=clock();
119         int n;
120         for(size_t i=0;i<100000;i++)
121              n=StrToIntGood(str);
122         now=clock()-now;
123         cout<<"StrToIntGood"<<" "<<n+1<<"time:"<<now<<endl;
124     }
125 
126     {
127         string stringSize("124565");
128         size_t row=stringSize.size()-1;
129         size_t col=9;
130         int t=1;
131 
132         //vector<vector<int> > matrix(row,vector<int>(col)); 
133         for (size_t i=0;i<row;++i){
134             t=t*10;
135             for (size_t j=0;j<col;j++){
136                 ::matrix[i][j]=t*(j+1);
137             }
138         }
139 
140 
141         int n;
142         string s("124565");
143         now=clock();
144         for(size_t i=0;i<100000;i++)
145              n=StrToBetter(s);
146         now=clock()-now;
147         cout<<"StrToBetter"<<" "<<n+1<<"time:"<<now<<endl;
148         
149     }
150     
151     return 0;
152 
153 }

 

posted on 2008-12-22 17:49  风荷小筑  阅读(495)  评论(0)    收藏  举报