使用比分配更多的内存从食谱中分解代码; EDX或代码问题?

发布于 2025-02-11 04:20:59 字数 1257 浏览 3 评论 0原文

我正在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看轻我的陪伴 2025-02-18 04:20:59

以下代码导致无限循环。

while (j != string::npos)
        v.push_back(s.substr(i, i - j));

由于循环未封闭在括号中,因此仅按照的语句为循环是循环的一部分。变量j未在循环中更新,它永远不会等于string :: npos,从而导致无限循环。

另外,s.substr(i,i -j)是不正确的,因为i -j将给出负长度。它应该是s.substr(i,j -i)

以下循环应按预期工作

while (j != string::npos)
{
    v.push_back(s.substr(i, j - i));
    i = ++j;
    j = s.find(c, j);

    if (j == string::npos)
    {
        v.push_back(s.substr(i, s.length()));
    }
}

The following code causes an infinite loop.

while (j != string::npos)
        v.push_back(s.substr(i, i - j));

Since the while loop is not enclosed in braces, only the statement following the while loop is part of the loop. The variable j is not updated in the loop, it will never be equal to string::npos, causing an infinite loop.

Also, s.substr(i, i - j) is incorrect since i - j will give a negative length. It should be s.substr(i, j - i)

Following loop should work as expected

while (j != string::npos)
{
    v.push_back(s.substr(i, j - i));
    i = ++j;
    j = s.find(c, j);

    if (j == string::npos)
    {
        v.push_back(s.substr(i, s.length()));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文