English 中文(简体)
严格限制[长期]
原标题:strict string to int[long]
  • 时间:2010-06-17 21:16:41
  •  标签:
  • c++

难道我们有一种标准的方法,可以严格地将果园变成(或很长的)t,也就是说,只有当所有特性都是数字,并且能够适应(或长期)——某种方式是使用斜体等?

因此,“sbc45”、“4590k”、“56”、“56”在使用这一功能时应一律无效。

最佳回答

这是一个接近@GMan的版本,但它不接受婚前空间。 e.g. 101":

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <exception>

long strict_conversion(const std::string& number_string)
{
    long number;
    std::stringstream convertor;
    convertor << std::noskipws << number_string;
    convertor >> number;
    if( convertor.fail() || !convertor.eof() )
        throw std::runtime_error("The string didn t pass the strict conversion!");
    return number;
}

一分钟后,即为:

template <typename NumberType>
NumberType strict_conversion(const std::string& number_string)
{
    NumberType number;
    std::stringstream convertor;
    convertor << std::noskipws << number_string;
    convertor >> number;
    if( convertor.fail() || !convertor.eof() )
        throw std::runtime_error("The string didn t pass the strict conversion!");
    return number;
}
问题回答

您可使用<代码>strtol-将其标题改为投入中的第一个“error nature”,因此,您可以仅将其核对为 ,看看是否最终存在任何垃圾。 如果数值超出范围,errno就被设定为ERANGE,那么你也可以发现这一点。

唯一的告诫是,strtol将qui弃任何主要的白色空间。 可以通过将<条码>isspace应用于第一组投入加以核对。

我想我这样说:

long convert(std::string const &s) { 
    if (s.find_first_not_of("0123456789") != std::string::npos)
        throw std::invalid_argument("Cannot convert value");
    return strtol(s.c_str(), NULL, 10);
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签