为什么我的这个程序不起作用?有什么不对的? [英] Why didn't my this program work? What is wrong thing?

查看:101
本文介绍了为什么我的这个程序不起作用?有什么不对的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include   <   iostream  >  
#include < vector < span class =code-keyword>>
使用 namespace std;
int main()
{
vector< int> S;
cout<< S.size();
S.push_back( 23 );
S.push_back( 45 );
S.push_back( - 99 );
S [ 4 ] = 99 ;
cout<< ' |'<<< S.at( 0 )<< ' |'< < S.at( 1 )<< ' |'<<< S.at( 2 )<< ' |'<< S.at( 3 )<< ' |'<< S.at( 4 )< ;< ' |';
cout<< endl<< S.size();
return 0 ;
}





我的尝试:



i编译并运行它,但它没有显示任何内容。请帮帮我。我的程序出了什么问题?

解决方案

你的程序未定义,因为您访问不存在的元素。



特别是,以下行具有未定义的行为,因为索引4处的项目尚不存在。

 S [ 4 ] =  99 ; 



因此,您应该阅读文档:

vector :: operator [] - C ++参考 [ ^ ]

vector :: operator [ ^ ]



实际上,就在该行执行之前,你有3个项目在索引0,1 2.



鉴于此,下一行也是错误的,因为你试图在索引3和4处显示项目。因此你应该调用 push_back 两次添加这些项目。

 S.push_back( 0 );  //  无论您想要索引3 ...  
S.push_back( 99 ); // 而不是S [4] = 99;



顺便说一句,我建议你添加一些空格(和缩进)以便于阅读。以下是您的代码中的一些示例,它们具有更多可读性以及一些额外的空格:

 vector< int> S; 
// ...
S [ 4 ] = 99 ;
cout<< ' |'<< S.at( 0 )<< ' |'<< S.at( 1 )<< ' |'<< S.at( 2 )<< ' |'<< S.at( 3 )<< ' |'<< S.at( 4 )<< ' |';
cout<< endl<< S.size();
return 0 ;


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你接近一个bug。



[更新]

在你的代码中使用缩进,它有助于在代码变得复杂时阅读。

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int>S;
    cout<<S.size();
    S.push_back(23);
    S.push_back(45);
    S.push_back(-99);
    S[4]=99;
    cout<<'|'<<S.at(0)<<'|'<<S.at(1)<<'|'<<S.at(2)<<'|'<<S.at(3)<<'|'<<S.at(4)<<'|';
    cout<<endl<<S.size();
    return 0;
}



What I have tried:

i compiled and ran it,but it didn't show anything.Please,help me.what is wrong with my program?

解决方案

Your program is undefined because you access non-existing elements.

In particular, the following line has undefined behavior because item at index 4 does not yet exist.

S[4]=99;


Thus, you should read the documentation:
vector::operator[] - C++ Reference[^]
vector::operator[^]

In fact, just before that line get executed, you have 3 items at index 0, 1 and 2.

Given that, next line is also incorrect because you try to display items at index 3 and 4. Thus you should call push_back twice to add those items.

S.push_back(0);   // Whatever you want at index 3...
S.push_back(99);  // Instead of S[4] = 99;


By the way, I would recommand you to add some white space (and indentation) for readability. Here are a few example from your code that are more readable with some extra spaces:

vector<int> S;
//...
S[4] = 99;
cout << '|' << S.at(0) << '|' << S.at(1) << '|' << S.at(2) << '|' << S.at(3) << '|' << S.at(4) << '|';
cout << endl << S.size();
return 0;


You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

[Update]
Use indentation in your code, it helps reading when code become complicated.


这篇关于为什么我的这个程序不起作用?有什么不对的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆