本文介绍了将字符串分配给int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 我想知道是否有一种简单的方法可以将字符串分配给int。 I有一个结构,如: struct Values { int a; int b; } 我从文件中读取了2个字符串: string x =" 12"; string y =" 10"; 现在我想做这样的事情: 值val; val.a = x; val.b = y; 当然有不匹配的情况..如何解决?有一个简单的方法吗? Thanx求救! Ben 解决方案 " Ben" < CR ********* @ yahoo.com>在消息中写道 news:d9 ************************** @ posting.google.c om ... 大家好, 我想知道是否有一种简单的方法可以将字符串分配给int。 不,这两种类型不是兼容。然而 (就像它看起来你真的在问,是的 你可以转换 整数的文字表示(即一个数字字符序列) 一个整数类型。) 我有一个结构如: struct values { int a; int b; } 我从文件中读取了2个字符串: string x =" 12"; string y =" 10"; 现在我想做这样的事情:值val; val.a = x; val .b = y; 当然有不匹配...我该如何解决?有一个简单的方法吗? 有几种方法,简单是一个意见问题。 然后还有一些关于更好的意见。 一种方法是使用字符串流 (你需要#include< ; sstream> 使用一个): std :: istringstream iss(x +'''+ y); iss>> val.a>> val.b; 对于(imo)更灵活,健壮的方法,查看 ''strtol()''函数,由< cstdlib>宣布 -Mike Ben写道:大家好, 我想知道是否有一种简单的方法可以将字符串分配给 int。 我有一个结构如: struct values { int a; int b; } 我有2个字符串,我从文件中读取: string x =" 12"; string y =" 10" 现在我想做这样的事情:值val; val.a = x; val.b = y; 当然有不匹配的问题..如何解决?有没有简单的方法? Thanx求助! Ben 模板< typename T,typename TCHAR> T fromstring(const std :: basic_string< TCHAR>& s) { std :: basic_istringstream< TCHAR,std :: char_traits< TCHAR> ;, std :: allocator< TCHAR> > iss(s); T x; iss>> x; if(!iss) throw std :: invalid_argument(" Bad argument!"); return x; } 模板< typename T,typename TCHAR> std :: basic_string< TCHAR> tostring(const T& x) { std :: basic_ostringstream< TCHAR,std :: char_traits< TCHAR>, std ::分配器< TCHAR> > oss; oss<< x; if(!oss) throw std :: invalid_argument(" Bad argument!"); return oss.str (); } val.a = fromstring< int,char>(x); val.b = fromstring< int,char>(y); fromstring()可以类似的方式使用。 - Pete Petec写道: < snip> 模板< typename T,typename TCHAR> std :: basic_string< TCHAR> tostring(const T& x) { std :: basic_ostringstream< TCHAR,std :: char_traits< TCHAR>, std :: allocator< TCHAR> > oss; oss<< x; if(!oss) throw std :: invalid_argument(" Bad argument!"); return oss.str(); } val.a = fromstring< int,char>(x); val.b = fromstring< int,char>(y); fromstring()可以使用以类似的方式。 对不起,我的意思是tostring<>()可以以类似的方式使用。 :) - 皮特 Hi all,I would like to know if there is an easy way to assign a string to an int.I have a struct such as:struct Values {int a;int b;}I have 2 strings that i read from a file:string x = "12";string y = "10";Now I''d like to do something like this:Values val;val.a = x;val.b = y;There is of course a mis-match.. How do I fix it? Is there an easy way?Thanx for help!Ben 解决方案"Ben" <cr*********@yahoo.com> wrote in messagenews:d9**************************@posting.google.c om... Hi all, I would like to know if there is an easy way to assign a string to an int.No, those two types are not ''compatible''. However(as is what it appears you''re really asking, yesyou can convert the textual representation of aninteger (i.e. a sequence of digit characters) toan integer type.) I have a struct such as: struct Values { int a; int b; } I have 2 strings that i read from a file: string x = "12"; string y = "10"; Now I''d like to do something like this: Values val; val.a = x; val.b = y; There is of course a mis-match.. How do I fix it? Is there an easy way?There are a few ways, which is ''easy'' is a matter of opinion.Then there are also opinions about which is ''better''.One way is to use a stringstream(you''ll need to #include <sstream>to use one):std::istringstream iss(x + '' '' + y);iss >> val.a >> val.b;For a (imo) more flexible, ''robust'' method, look up the''strtol()'' function, declared by <cstdlib>-MikeBen wrote: Hi all, I would like to know if there is an easy way to assign a string to an int. I have a struct such as: struct Values { int a; int b; } I have 2 strings that i read from a file: string x = "12"; string y = "10"; Now I''d like to do something like this: Values val; val.a = x; val.b = y; There is of course a mis-match.. How do I fix it? Is there an easy way? Thanx for help! Bentemplate<typename T, typename TCHAR>T fromstring(const std::basic_string<TCHAR>& s){std::basic_istringstream<TCHAR, std::char_traits<TCHAR>,std::allocator<TCHAR> > iss(s);T x;iss >> x;if(!iss)throw std::invalid_argument("Bad argument!");return x;}template<typename T, typename TCHAR>std::basic_string<TCHAR> tostring(const T& x){std::basic_ostringstream<TCHAR, std::char_traits<TCHAR>,std::allocator<TCHAR> > oss;oss << x;if(!oss)throw std::invalid_argument("Bad argument!");return oss.str();}val.a = fromstring<int, char>(x);val.b = fromstring<int, char>(y);fromstring() can be used in a similar way.- PetePetec wrote:<snip> template<typename T, typename TCHAR> std::basic_string<TCHAR> tostring(const T& x) { std::basic_ostringstream<TCHAR, std::char_traits<TCHAR>, std::allocator<TCHAR> > oss; oss << x; if(!oss) throw std::invalid_argument("Bad argument!"); return oss.str(); } val.a = fromstring<int, char>(x); val.b = fromstring<int, char>(y); fromstring() can be used in a similar way.Sorry, I meant tostring<>() can be used in a similar way. :) - Pete 这篇关于将字符串分配给int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!