c++/动态分配

发布于 2025-01-22 15:55:12 字数 727 浏览 1 评论 0原文

创建*std作为全局变量。

创建*std作为多个num输入时。

由于num包含在main()中,因此未执行此代码。

main(Main()获取num输入时,是否有一种方法可以将*std设置为全局变量?

#include <iostream>
#include <string>
using namespace std;

struct s1 {
    string str1;
};

s1* Std = new s1[Num];

int main()
{
    cout << "How many [Std] do you want to create? : ";
    int Num;
    cin >>Num;

    for (int i = 0; i < Num; i++)
    {
        getline(cin, Std[i].str1);
        cout << "\nStd["<<i<<"].str1 : " << Std[i].str1<<"\n\n";
    }
}

Create *Std as a global variable.

Create *Std as many Num as you enter.

This code is not executed because Num is contained within main().

Is there a way to set *Std as a global variable while getting Num input from main()?

#include <iostream>
#include <string>
using namespace std;

struct s1 {
    string str1;
};

s1* Std = new s1[Num];

int main()
{
    cout << "How many [Std] do you want to create? : ";
    int Num;
    cin >>Num;

    for (int i = 0; i < Num; i++)
    {
        getline(cin, Std[i].str1);
        cout << "\nStd["<<i<<"].str1 : " << Std[i].str1<<"\n\n";
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

贱人配狗天长地久 2025-01-29 15:55:12

在声明时不必初始化变量。您可以将声明与分配分开。

在这种情况下,您可以将std作为一个全局变量,如果您真的想(尽管, Globals通常是在上皱眉,在这种情况下,这是不必要的main()num已读取,例如:

#include <iostream>
#include <string>
using namespace std;

struct s1 {
    string str1;
};

s1* Std;

int main()
{
    cout << "How many [Std] do you want to create? : ";
    int Num;
    cin >>Num;

    Std = new s1[Num];

    for (int i = 0; i < Num; i++)
    {
        getline(cin, Std[i].str1);
        cout << "\nStd[" << i << "].str1 : " << Std[i].str1 << "\n\n";
    }

    delete[] Std;
}

说,您应该使用标准 std :: vector 当您需要动态数组时,容器,例如:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct s1 {
    string str1;
};

vector<s1> Std;

int main()
{
    cout << "How many [Std] do you want to create? : ";
    int Num;
    cin >>Num;

    Std.resize(Num);

    for (int i = 0; i < Num; i++)
    {
        getline(cin, Std[i].str1);
        cout << "\nStd[" << i << "].str1 : " << Std[i].str1 << "\n\n";
    }
}

Variables don't have to be initialized at the time they are declared. You can separate declaration from assignment.

In this case, you can leave Std as a global variable, if you really want to (though, globals are generally frowned upon, and in this case it is unnecessary since there are no other functions wanting to access Std), however you must move the new[] statement into main() after Num has been read in, eg:

#include <iostream>
#include <string>
using namespace std;

struct s1 {
    string str1;
};

s1* Std;

int main()
{
    cout << "How many [Std] do you want to create? : ";
    int Num;
    cin >>Num;

    Std = new s1[Num];

    for (int i = 0; i < Num; i++)
    {
        getline(cin, Std[i].str1);
        cout << "\nStd[" << i << "].str1 : " << Std[i].str1 << "\n\n";
    }

    delete[] Std;
}

That being said, you should use the standard std::vector container instead whenever you need a dynamic array, eg:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct s1 {
    string str1;
};

vector<s1> Std;

int main()
{
    cout << "How many [Std] do you want to create? : ";
    int Num;
    cin >>Num;

    Std.resize(Num);

    for (int i = 0; i < Num; i++)
    {
        getline(cin, Std[i].str1);
        cout << "\nStd[" << i << "].str1 : " << Std[i].str1 << "\n\n";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文