std:std::stringg:npos的值是多少?有定义吗

std:string:npos的值是多少?有定义吗_百度知道
std:string:npos的值是多少?有定义吗
提问者采纳
一般是-1:container_type,用来表示不存在的位置npos 是一个常数:size_type 许多容器都提供这个东西,就不会存在移植的问题了:,这样做。取值由实现决定:,类型一般是std
来自团队:
其他类似问题
为您推荐:
硬质合金的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁std:string:npos的值是多少?有定义吗?解决思路_String不可以赋值吗?该怎么解决_一个简单的数组问提?该如何解决__脚本百事通
稍等,加载中……
^_^请注意,有可能下面的2篇文章才是您想要的内容:
std:string:npos的值是多少?有定义吗?解决思路
String不可以赋值吗?该怎么解决
一个简单的数组问提?该如何解决
std:string:npos的值是多少?有定义吗?解决思路
std::string::npos的值是多少?有定义吗?如题:
据说是大于所有有效下标的一个值,那这个值有定义是多少吗?
如果在程序中把-1作为它来用可以吗?
if(string.find() == -1)
cout &&"no found.";
------解决方案--------------------ISO C++03
static const size_type npos = -1;
相当于static_cast&size_type&(-1)。应该注意这里的size_type是allocator决定的无符号数。
LZ这么做可以,不过还是写成npos明确一点。------解决方案---------------------1,但是完全没有必要直接和这个-1进行比较。------解决方案--------------------永远不要依赖于npos的实际值。------解决方案--------------------不同的编译系统,NOPS的值是不同的,通常为-1。最好把它当作常量来使用。------解决方案--------------------我觉得还是直接用npos比较好。。------解决方案--------------------
// NB: This is an unsigned type, and thus represents the maximum
// size that the allocator can hold.
Value returned by various member functions when they fail.
static const size_type npos = static_cast&size_type&(-1);
自己右键 就可以查头文件
看头文件里的注解
//注:这是一个无符号的类型,是tring容易可容纳的最大大小。------解决方案--------------------msdn中是这样定义的:static const size_type npos = -1;
String不可以赋值吗?该怎么解决
String不可以赋值吗??void
__fastcall
TForm2::okbuttonClick(TObject
ttt= "123 ";
puNow-& FileName=
我在一个按钮写上这代功能;
是想把puNow-& FileName重写赋值,但怎么会出现内存错误呢?
puNow的指针结构体:
//普通文件的目录项
//文件类型,0为只读,1为可读可写
//文件长度
//看是否是文件夹,当为1是文件
//文件夹链表
}UFDNode,*UFDL------解决方案--------------------建议用 char[100] 之类的描述
链表内存为连续固定字节内存,而 String 类型是一个可变对象
------解决方案--------------------puNow-& FileName=
很有可能你只是定义的指针,所以不能赋值
------解决方案--------------------是不是puNow没实例化
------解决方案--------------------puNow的内存分配了码?把错误信息贴出来
------解决方案--------------------是呀,加一句实例化
puNow = new UFD; ------解决方案--------------------什么错误?
------解决方案--------------------八成是没分配内存指向野指针了
一个简单的数组问提?该如何解决
一个简单的数组问提??一个数组里有这样的一些数。
0,1,2,3,4,4;
怎样才可以把他按数值分成两等分
,用变成的方法,可以分为
3,4,0;两部分,
但是对与其他的也可以分,如:
1,1,1,2,5,0;可以分成
1,1,1,2和5,0
1,1,1,2,0;和
就是要达到分成两份后
,数值想加相等。
那为大哥可以用变成来实现;谢谢了。------解决方案--------------------去看看这个帖子吧,各位老大讲的很清楚了
http://community.csdn.net/Expert/topic/.xml?temp=.8574182
------解决方案--------------------可以有很多做法,我记得最近一次是用随机交换做的,呵呵.
------解决方案--------------------一种简单的办法,先求出数值和
之后求出平均值(/2)
然后探测一组和为平均值的数列
这个应该很容易实现吧!
------解决方案--------------------一种简单的办法,先求出所有数值和
之后求出整体的半值(sum/2)
然后探测一组和为半值的数的序列
这个应该很容易实现吧!
------解决方案--------------------学习!
------解决方案-------------------- "关键在于怎么样才可以探测出一组和为半值的数的数列,,,!! "
这个肯定有无数方法实现,
说个最简单、一下子想到的,没仔细考虑,可能有问题,
但应该这个问题很容易搞定,抛砖引玉了
用栈结构去穷举
从最小数开始压栈,将当前栈内元素和,与栈外最小元素相加
若小于“半值”,栈外最小元素入栈;
若大于“半值”,栈顶元素出栈,再与刚才栈外的最小元素求和,循环;
若等于则输出
------解决方案--------------------若所有元素用尽(就是说当前栈外最小大于半值)
或者栈为空
则将次小数第一个入栈,如此循环
如果您喜欢IT行业或者对IT行业感兴趣,想开拓技术视野,欢迎加入本站官方QQ群:,在群里认识新朋友和交流技术^_^
本站联系邮箱:c++ - std::string.npos validity - Stack Overflow
to customize your list.
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
J it only takes a minute:
Join the Stack Overflow community to:
Ask programming questions
Answer and help your peers
Get recognized for your expertise
Was std::string.npos ever valid? (As opposed to the correct std::string::npos.)
I am seeing it a lot in an old project I'm working on, and it does not compile with VS2010.
Is it something from the pre-standard days?
207k25327457
190k32281517
No, std::string.npos was never valid, and no, it's not something from the pre-standard days.
I see other answers mentioning that MSVC has allowed that notation.
However, MSVC is not a very compliant compiler. For example, it lets you freely bind a temporary to a reference to non-const. For another example, for Windows GUI subsystem applications you have to use not well-documented switches to make it accept a standard main. Much has improved since Microsoft hired Herb Sutter (and other guy that I don't remember the name of right now) to fix up their monstrous compiler. And in relative terms it has been really great, but in absolute terms, well that compiler is still a bit lacking.
190k32281517
86.3k682167
Once upon a time, when C with classes was developed, the :: syntax had not yet been invented. The code looked like:
void X.f() // a dot! see D&E 2.3
However, std namespace didn't exist those days. Therefore, I suspect std::string.npos is purely Microsoft's extension (or a bug?). It might be inspired by the old syntax, and might be not.
35.3k967128
Access to any static member via class name and dot was unfortunately allowed by prior versions of MSVC.
#include &iostream&
int main()
std::cout && A.a;
This code is happily accepted by MSVC9.0 with a warning
warning C4832: token '.' is
illegal after UDT 'A'
The C++ standard obviously disallows access to a static member via className.memberName (although it is perfectly legal to access a static member via an object object.staticMemberName).
My common sense tells me that if MSVC is aware that this is not standard and gives a warning, then we can turn that extension off. We go to Project Propertied -& C/C++ -& Language and set Disable Language Extensions to Yes. Do you think anything changes? Of course not, the compiler still accepts the illegal code with the same warning. I sometimes wonder what Disable Language Extensions actually does...
70.3k27193323
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
The week's top questions and answers
Important community announcements
Questions that need answers
By subscribing, you agree to the
Stack Overflow works best with JavaScript enabledstring::npos
The&string&class provides six
search functions, each named as a variant
of&find. The
operations all return astring::size_type&value that
is the index of where the match occurred, or a special value
named&string::nposif there is no match.
The&string&class
defines&npos&as a value that is
guaranteed to be greater than any valid index.
string 类提供了 6 种查找函数,每种函数以不同形式的 find 命名。这些操作全都返回 string::size_type
类型的值,以下标形式标记查找匹配所发生的位置;或者返回一个名为 string::npos 的特殊值,说明查找没有匹配。string
类将 npos 定义为保证大于任何有效下标的值。
pos=str.find_first_of("h");
if(pos!=string::npos)
//npos是一个常数,用来表示不存在的位置,类型一般是std::container_type::size_type&
//许多容器都提供这个东西。取值由实现决定,一般是-1,这样做,就不会存在移植的问题了。npos表示string的结束位子,
//是string::type_size 类型的,也就是find()返回的类型。
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

我要回帖

更多关于 std::string 的文章

 

随机推荐