无符号整数(string::size_type,size_t,unsigned,long unsigned)

string::size_type size_t unsigned long unsigned

这四种数据类型在C++语言环境下均为无符号整数,但是却又各不相同,这里介绍下它们各自之间的关系和区别。

string::size_type

标准库string中的成员类型(member type),以实现标准库类型和机器的无关性,能够存下任意string对象的大小,是一种无符号类型。

标准库string的成员函数(size(),查找函数)的返回值类型均为string::size_type

在用下标访问元素时,string使用string::size_type作为下标类型。

string::size_type它在不同的机器上,长度是可以不同的,并非固定的长度。但只要你使用了这个类型,就使得你的程序适合这个机器与实际机器匹配。与之类似的有vector::size_type

size_t

cstddef头文件中定义的一种与机器实现有关的无符号整数类型,其能够表示任意数组的大小。

size_tsize_type的区别和联系

在C++Primer一书中,并未指出size_type(vector::size_type,string::size_type)具体为什么类型,只是指明其为无符号类型。能够存放任意string或者vector的对象大小。但是在实际中,size_tsize_type是几乎没有什么差别的。在C++Reference string中指明成员类型size_type的定义即为size_tvector::size_type也一样。

string::npos


string::size_type所能表示的最大值,其在不同的机器环境下有着不同的值,当使用标准库string中的查找(find,rfind,find_first_of,find_first_not_of,find_last_of,find_last_not_of)函数时,如果无法找到指定的字符串,返回string::npos。实际中,其值等于string::size_type i = -1i的值。

unsigned


unsigned int,无符号整数类型,不同的机器上其能够表示的范围不同。但是最少要求两个字节存储一个unsigned类型的整数,因此unsigned类型最小的最大值为65535,而在实际系统中可能会更大,如采用四个字节存储unsigned的数据类型的时候,最大值为4294967295。C语言标准库中的limits.h头文件定义了unsinged int的最大值宏——UINT_MAX。

long unsigned


long unsigned int,无符号长整数类型,不同的机器上其能够表示的范围不同。但是最少要求两个字节存储一个unsigned long类型的整数,因此unsigned long类型最小的最大值为4294967295,而在实际系统中可能会更大。与unsigned类似,C语言标准库中的limits.h头文件定义了unsinged int的最大值宏——ULONG_MAX。

代码分析

#include <vector>
#include <string>
#include <iostream>
#include <cstddef>

using std::vector;
using std::string;
using std::cout;
using std::endl;
using std::cin;

int main(int argc, char const *argv[])
{
    unsigned i          = -1;
    long unsigned l     = -1;
    string::size_type j = -1;
    size_t k            = -1;   

    cout << "sizeof(unsigned): " << sizeof(unsigned) << endl;
    cout << i << endl;
    cout << "sizeof(long unsigned): " << sizeof(long unsigned) << endl;
    cout << l << endl;
    cout << "sizeof(string::size_type): " << sizeof(string::size_type) << endl;
    cout << j << endl;
    cout << "sizeof(size_t): " << sizeof(size_t) << endl;
    cout << k << endl;

    return 0;
}

在ubtun64位的机器上程序的输出结果为:

sizeof(unsigned): 4
4294967295
sizeof(long unsigned): 8
18446744073709551615
sizeof(string::size_type): 8
18446744073709551615
sizeof(size_t): 8
18446744073709551615

在vs2010下程序的执行结果为:

注意事项

在实际中,使用unsigned或者long unsigned来存放一个stringfind或者类似的成员函数,来存放返回的值是非常危险的行为,应该使用相对应的string::size_type来存放。
如下:

#include <string>
#include <iostream>

using std::string;
using std::cout;
using std::endl;
using std::cin;

int main(int argc, char const *argv[])
{
    string str("ab2c3d7R4E6");
    string num("0123456789");

    string::size_type offset = 0;
    unsigned i;
    while (( i = str.find_first_of(num,offset)) != string::npos)
    {
        cout << str[i] << " ";
        offset = i + 1;
    }

    cout << endl;
    return 0;
}

上述代码在vs2010环境下是可以正确运行的,但是在ubtuntu64环境下,总是发生段错误,而实际原因就是用unsigned i来存放find_first_of的返回值,显然在此环境下i最大值只能是4294967295,而string::npos的值则为18446744073709551615。因此会发生错误。而在vs2010下unsigned类型可以存放string::size_type的所有值的,能够正确的存放 string::npos的值,因此能够执行成功。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代码解释:void CopleyAmplifier::SetNewPVTMotionStartTime(boost::posix_time::ptime time,CouchTrjType pvt_point) { //Record the time stamp and data. m_bool_pvt_started = true; m_start_motion_time_us = PosixTime2Integer<unsigned long long>(time); m_last_pvt_data.p = m_start_pos; //Send the last dummy data calculated by the motion start time. ptime current_time = microsec_clock::universal_time(); ptime couch_time = Integer2PosixTime<unsigned long long>(pvt_point.t, current_time); ptime couch_to_L1_time = Integer2PosixTime<unsigned long long>(pvt_point.timeReachToBuffer, current_time); unsigned char next_point_time = round((pvt_point.t-m_start_motion_time_us)/1000.0)-m_total_motion_time_ms; if(next_point_time<4) { GcLogInfo(m_log_id, __FUNCTION__, "<CopleyStartPVT>Motion start time:%s. First couch time:%s.First couch to L1 time:%s.", boost::posix_time::to_simple_string(time).c_str(), boost::posix_time::to_simple_string(couch_time).c_str(), boost::posix_time::to_simple_string(couch_to_L1_time).c_str()); GcLogInfo(m_log_id, __FUNCTION__, "next_point_time: %d.",next_point_time); BOOST_THROW_EXCEPTION(AxisException() <<Axis_Error_Msg("Start PVT time failed! No enough time for First PVT data!")); } AmpPVTData dummy_data = {next_point_time,0,0}; //Send the left dummy data. dummy_data.time = next_point_time; Gantry::Array seg_cmd = ComposePVTRawData(dummy_data,m_next_pvt_index,1); GcLogDebugExpect(m_need_trace, m_log_id, __FUNCTION__, "<CopleyStartPVT>The %dth PVT dummy data.", m_next_pvt_index); WriteSDO(Gantry::ODAddress(COPLEY_PVT_DATA, 0), (unsigned long long)seg_cmd.GetValue<unsigned long long>()); GcLogInfo(m_log_id, __FUNCTION__, "<CopleyStartPVT>Motion start time:%s. First couch time:%s.First couch to L1 time:%s.", boost::posix_time::to_simple_string(time).c_str(), boost::posix_time::to_simple_string(couch_time).c_str(), boost::posix_time::to_simple_string(couch_to_L1_time).c_str()); m_total_motion_time_ms += dummy_data.time; m_lasttrj_segments.push_back(seg_cmd.GetValue<unsigned long long>()); ++m_next_pvt_index; GcLogInfo(m_log_id, __FUNCTION__, "<CopleyStartPVT>Motion Started. Start position %f mm.", pvt_point.p); }
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值