我有以下代码: -
string name = "hello world";
size_t find = name.find("world");
if (find != string::npos) {
cout<<"match found at "<<find;
}
else{
cout<<find;
}
此程序正常工作并按预期打印6输出。
但如果我将其更改为size_t find = name.find('\n');
它将垃圾值打印为18446744073709551615
。找不到函数打印垃圾值时找不到匹配的字符串??
答案 0 :(得分:2)
来自http://www.cplusplus.com/reference/string/string/find/
返回值
第一场比赛的第一个角色的位置。 如果未找到匹配项,则函数返回string :: npos。
size_t是无符号整数类型(与成员类型相同) 串:: size_type的)。
因此,您的函数会打印std :: string :: npos,例如在MSVS标准库实现中
basic_string<_Elem, _Traits, _Alloc>::npos =
(typename basic_string<_Elem, _Traits, _Alloc>::size_type)(-1);
64位系统上的最大unsigned int值是:18,446,744,073,709,551,615,请参阅https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx
您可以将代码更改为
string name = "hello world";
size_t find = name.find("world");
if (find != string::npos) {
cout<<"match found at "<<find;
}
else {
cout<<"match not found";
}
答案 1 :(得分:1)
它打印垃圾值为18446744073709551615.找不到函数打印垃圾值时找不到匹配的字符串??
不,std::basic_string::npos
无法打印。相反,你打印出它的返回值。它不是垃圾价值,只是static const size_type npos = -1;
的价值。
请参阅std::basic_string::npos
的定义:
size_type
这是一个特殊值,等于
java.lang.NoClassDefFoundError: com/android/util/PropertiesMap at com.android.layoutlib.bridge.android.BridgeContext.createStyleBasedTypedArray(BridgeContext.java:940) at com.android.layoutlib.bridge.android.BridgeContext.obtainStyledAttributes(BridgeContext.java:638) at android.content.res.Resources_Theme_Delegate.obtainStyledAttributes(Resources_Theme_Delegate.java:71) at android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java:1436) at android.widget.TextView.<init>(TextView.java:761) at android.widget.TextView.<init>(TextView.java:704) at android.widget.TextView.<init>(TextView.java:700) at com.android.layoutlib.bridge.MockView.<init>(MockView.java:50) at com.android.layoutlib.bridge.MockView.<init>(MockView.java:45) at com.android.layoutlib.bridge.MockView.<init>(MockView.java:41) at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:163) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:858) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70) at android.view.LayoutInflater.rInflate(LayoutInflater.java:834) at android.view.LayoutInflater.inflate(LayoutInflater.java:492) at com.android.layoutlib.bridge.bars.CustomBar.<init>(CustomBar.java:95) at com.android.layoutlib.bridge.bars.StatusBar.<init>(StatusBar.java:67) at com.android.layoutlib.bridge.impl.Layout.createStatusBar(Layout.java:222) at com.android.layoutlib.bridge.impl.Layout.<init>(Layout.java:144) at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:297) at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:429) at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:350) at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:520) at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:508) at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:967) at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:508) at com.android.tools.idea.rendering.RenderTask.access$600(RenderTask.java:75) at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:620) at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:617) at com.android.tools.idea.rendering.RenderService.runRenderAction(RenderService.java:371) at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:617) at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:639) at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.java:654) at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1700(AndroidLayoutPreviewToolWindowManager.java:82) at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7$1.run(AndroidLayoutPreviewToolWindowManager.java:596) at com.intellij.openapi.progress.impl.CoreProgressManager$2.run(CoreProgressManager.java:142) at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:446) at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:392) at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:54) at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:127) at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7.run(AndroidLayoutPreviewToolWindowManager.java:591) at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:337) at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:327) at com.intellij.util.ui.update.MergingUpdateQueue$3.run(MergingUpdateQueue.java:271) at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:286) at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:244) at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:234) at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238) at com.intellij.util.Alarm$Request$1.run(Alarm.java:352) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)
类型可表示的最大值。
答案 2 :(得分:0)
将您的其他语句更改为打印&#34;未找到&#34;。
您的代码正在打印find
的返回值,如果找不到则返回std::string::npos
。