使用比分配更多的内存从食谱中分解代码; EDX或代码问题?
我正在IBM通过EDX参加C ++编程课程。该计划的大部分是动手练习,其中一个人撰写代码以满足规格,以提供所需的输出输入,并提供两者的样本。在线编译器用于执行和评估代码。
我从厨师书中拿出代码以拆分字符串。据我所知,代码应该起作用。但是,当我编译它时,编译器的内存不足,我相信将其分配给了512 MB。由于编译器是提供的服务,因此我无法将内存分配或更改任何设置。这也是我需要用于该课程的编译器。我在课程讨论板上询问了我的代码为什么会引起问题,但我没有回答。我想知道问题是否是编译器和平台,或者我的代码中是否有问题。
我将代码削减到了分裂字符串所需的内容,这导致了内存分配问题。
#include <iostream>
#include <string.h>
#include <vector>
#include <functional>
using namespace std;
void split(const string &s, char c, vector<string> &v)
{
string::size_type i = 0;
string::size_type j = s.find(c);
while (j != string::npos)
v.push_back(s.substr(i, i - j));
i = ++j;
j = s.find(c, j);
if (j == string::npos)
{
v.push_back(s.substr(i, s.length()));
}
}
int main()
{
// Fill the code here
// Strings
string prompt1 = "Enter name: ";
string prompt2 = "Enter roll number: ";
string prompt3 = "Enter Date of Birth [DD MM YY] format: ";
vector<string> v;
string s = "Something To Put Here";
split(s, ' ', v);
for (int i = 0; i < v.size(); ++i)
{
cout << v[i] << endl;
}
return 0;
}
那么,是代码还是平台?
I am taking a course in c++ programming by IBM through edX. A majority of the program are hands-on exercises where one writes code to meet specifications, for a desired output given input, with samples of both provided. An online compiler is used to execute and evaluate the code.
I took code from a cook-book to split a string. As far as I can tell, the code should work. When I compile it though, the compiler runs out of memory, which I believe has 512 mb allocated to it. As the compiler is a service provided, I cannot allocate memory to it or change any settings. It is also the compiler I am required to use for the course. I asked on the course discussion board why my code is causing problems, but I have had no responses. I would like to know if the problem is the compiler and platform, or if I have a problem in my code.
I pared down the code to just what is needed for splitting a string, which is causing the memory allocation problems.
#include <iostream>
#include <string.h>
#include <vector>
#include <functional>
using namespace std;
void split(const string &s, char c, vector<string> &v)
{
string::size_type i = 0;
string::size_type j = s.find(c);
while (j != string::npos)
v.push_back(s.substr(i, i - j));
i = ++j;
j = s.find(c, j);
if (j == string::npos)
{
v.push_back(s.substr(i, s.length()));
}
}
int main()
{
// Fill the code here
// Strings
string prompt1 = "Enter name: ";
string prompt2 = "Enter roll number: ";
string prompt3 = "Enter Date of Birth [DD MM YY] format: ";
vector<string> v;
string s = "Something To Put Here";
split(s, ' ', v);
for (int i = 0; i < v.size(); ++i)
{
cout << v[i] << endl;
}
return 0;
}
So, is it the code or is it the platform?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下代码导致无限循环。
由于
循环未封闭在括号中,因此仅按照
的语句为
循环是循环的一部分。变量j
未在循环中更新,它永远不会等于string :: npos
,从而导致无限循环。另外,
s.substr(i,i -j)
是不正确的,因为i -j
将给出负长度。它应该是s.substr(i,j -i)
以下循环应按预期工作
The following code causes an infinite loop.
Since the
while
loop is not enclosed in braces, only the statement following thewhile
loop is part of the loop. The variablej
is not updated in the loop, it will never be equal tostring::npos
, causing an infinite loop.Also,
s.substr(i, i - j)
is incorrect sincei - j
will give a negative length. It should bes.substr(i, j - i)
Following loop should work as expected