`
orange.lpai
  • 浏览: 89862 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Summerizing out:string to int,int to string

阅读更多
转处百度NLP部门jijuhttp://super-jiju.spaces.live.com/?_c11_BlogPart_BlogPart=blogview&_c=BlogPart&partqs=amonth%3d12%26ayear%3d2008

1.
integer = atoi( my_string.c_str() );


2.
#include <iostream>
#include <sstream>//用这个类;

int main()
{
using namespace std;

string s = "1234";
stringstream ss(s); // Could of course also have done ss("1234") directly.

int i;
ss >> i;
cout << i << endl;

return 0;
}


3
.#include <boost/lexcal_cast.hpp>

// ...
int i = 42;
std::string s = boost::lexical_cast<std::string>(i);
int j = boost::lexical_cast<int>(s)

4.
#include <iostream>
#include <limits>

using namespace std;

int main ( int argc, char* argv[] )
{
int i;
cout << "enter an interger (or some random garbage): ";
cout.flush();
while ( (cin >> i).fail() )
{
// clear the error flag in the cin object
cin.clear();
// discard the erroneous user input
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "ooops! please try again: ";
cout.flush();
}
cout << "thank you for entering " << i << endl;
}

This technique should be applicable to other stream objects and other non-integer types.


http://www.cplusplus.com/reference/iostream/ios/fail.html
http://www.cplusplus.com/reference/iostream/ios/clear.html
http://www.cplusplus.com/reference/iostream/istream/ignore.html
http://www.cplusplus.com/reference/iostream/streamsize.html
http://publib.boulder.ibm.com/infocenter/comphelp/v9v111/index.jsp?topic=/com.ibm.xlcpp9.aix.doc/standlib/header_limits.htm
5.
string IntToString(int intValue) { 
  char *myBuff; 
  string strRetVal; 

  // Create a new char array 
  myBuff = new char[100]; 

  // Set it to empty 
  memset(myBuff,'\0',100); 

  // Convert to string 
  itoa(intValue,myBuff,10); 

  // Copy the buffer into the string object 
  strRetVal = myBuff; 
   
  // Delete the buffer 
  delete[] myBuff; 

  return(strRetVal); 
}


6.最后一个偶认为最好

int i=4507;   // any value can be assigned
string s;
char b[MAX];  // max is the maximum no of digits in a number

sprintf(b,"%d",i);// C-style string formed without null

s+=b; //c++ string(c++ STL string) is not necessarily null terminated

标准 C I/O
sprintf
语法:
 #include <stdio.h>
  int sprintf( char *buffer, const char *format, ... );

sprintf()函数和printf()类似, 只是把输出发送到buffer(缓冲区)中.返回值是写入的字符数量.
分享到:
评论

相关推荐

    C++ 模板写的短小字符串类,用于替换字符数组和std::string

    TinyString& operator = (const std::string& str); TinyString& operator = (const char* s); TinyString& operator = (char ch); template TinyString& operator += (const TinyString&lt;K&gt;& other); ...

    Tenyxy.zip

    练习1(基础): 建立一个学生类,其中... 第三个构造方法Person(String n, int a, String s, String m)设置类的name, age ,school和major属性; (3)在main方法中分别调用不同的构造方法创建的对象,并输出其属性值

    无法解析的外部符号”private: char * __cdecl cv::String::allocate(unsigned __int64)” (?allocate@String@cv@@AEAA

    1&gt;save-image-D435.obj : error LNK2001: 无法解析的外部符号 “private: char * __cdecl cv::String::allocate(unsigned __int64)” (?allocate@String@cv@@AEAAPEAD_K@Z) 1&gt;save-image-D435.obj : error LNK2001: ...

    Arduino项目开发 Strings_StringToInt_StringToInt.pdf

    Arduino项目开发 Strings_StringToInt_StringToInt.pdf 学习资料 复习资料 教学资源

    CString string char 之间的相互转换

    char* string_to_char(std::string str) char* string_to_char_Ex(std::string& str) template void other_to_string(T value,std::string& s) int CString_unicode_to_char(CString str,char* buff) CString char_...

    elasticdump报错

    6: v8::internal::Handle&lt;v8::internal::String&gt; v8::internal::JsonParser&lt;false&gt;::SlowScanJsonString&lt;v8::internal::SeqTwoByteString, unsigned short&gt;(v8::internal::Handle&lt;v8::internal::String&gt;, int, int)...

    ini 文件操作

    int32_t readInteger(std::string section, std::string key, int32_t defaultValue); float readFloat(std::string section, std::string key, float defaultValue); bool readBoolean(std::string section, std...

    StringAPI.java

    Java String 类型 API 测试代码 1.String和char[]之间的转换 ...String substring(int beginIndex, int endIndex) :返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串。

    StringToInt

    简单的使用string,通过ASCII码值减去'0'就可以得到数字.在加上相应的权值即可.

    Delphi_3_recive_send_batch_simple_sms_

    to_: ArrayOfString;fromtext: string; const isflash: Boolean): ArrayOfString;function SendSimpleSMS2(const usernamepasswordto_fromtext:string;isflash: Boolean): string;function SendSms(usernamepassword...

    Super string 库

    //cstring change to ansi string //-------------判断字符串类型-------------- bool is_a_double_type_string(CString str); //-------------字符串复杂操作-------------- int break_string(std::...

    Java程序设计基础:String类的常用方法(一.pptx

    String message = “Welcome to Java”; System.out.print(message.length()); //输出字符串长度15 返回字符串中字符的个数,即长度。中文、英文都算作一个字符。 其语法形式如下:字符串名.length(); 例1:在某系统...

    AS3正则表达式工具类

    chSt(str:String,minL:int,maxL:int):限制为中文字符 并限制字符长度 minL maxL 返回Boolean enSt(str:String,minL:int,maxL:int):限制为英文 并限制字符长度 minL maxL 返回Boolean checkEmail(str:String):email...

    string_int_label_map_pb2.py

    下载的protos包里只有string_int_label_map.proto这个文件,没有string_int_label_map_pb2.py文件,运行时会出现错误。自己去生成pb2文件比较麻烦。上传的这个string_int_label_map_pb2.py本机可正常使用。

    NCDDE服务连接DDE封装的API,ddehelper

    NCDDE服务连接DDE封装的API,ddehelper 使用c++语言 int GetData(LPSTR lpszDataName,std::string & data);... int DDEAdapter::TryRequest(std::string item, int format, int timeout, std::string &data);

    java计算四舍五入,float转int,string转int做计算

    提供两个思路,一个是float转int这种。另外就是就是string转int的除法计算。 这只是两个简单的小例子。可以借鉴看看。

    Springmvc : Failed to convert property value of type 'java.lang.String' to int

    NULL 博文链接:https://never-forget.iteye.com/blog/1833801

    JAVA编程基础-05面向对象基础-zhouxl.pdf

    JAVA编程基础-05面向对象基础-zhouxl.pdf

    c++通用库针对不同平台封装(win)

    int checkProcess(std::string name); //通过路径和名称检测进程是否运行中 返回[运行中/非运行中] 只有一个 bool checkProcess(std::string path, std::string name); //通过PID检测进程是否运行中 返回[运行...

Global site tag (gtag.js) - Google Analytics