【问题标题】:vector<string> in a struct doesn't work properly结构中的 vector<string> 无法正常工作
【发布时间】:2019-11-15 14:52:53
【问题描述】:

我声明了一个vector&lt;string&gt;,但我什至无法编译它。我尝试了很多方法,但都没有奏效。 我正在尝试写出 x.surname.push_back(word)[i] 但它确实写错了,我不知道如何正确编写它并使其能够编译。

#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int number, i = 0;
  string word;
  struct donators {
    vector<string> surname;
    vector<int> amount;
  } x;

  cout << "How many donators do you want to register? " << endl;
  cin >> number;

  for (i = 0; i < number; i++) {
    cout << "Surname: ";
    cin >> word;
    x.surname.push_back(word)[i];

    cout << "Amount: ";
    x.amount.push_back(i);
    cin >> x.amount[i];
  }
  cout << "OUR GORGEUS DONATORS: " << endl;
  for (i = 0; i < number; i++) {

    if (x.amount[i] >= 10000) {
      cout << "Surname: " << x.surname(word)[i];
      cout << "Amount: " << x.amount[i] << endl;
    }

    else if (x.amount[i] < 10000) {
      cout << "Lack of surnames!" << endl;
    }
  }
  cout << "OUR CASUAL DONATORS: " << endl;

  for (i = 0; i < number; i++) {

    if (x.amount[i] < 10000) {
      cout << "Surname: " << x.surname(word)[i];
      cout << "Amount: " << x.amount[i] << endl;
    } else if (x.amount[i] >= 10000) {
      cout << "Lack of surnames!" << endl;
    }
  }

  return 0;
}

还有一件事。如何造句“没有姓氏!”写一次?在某些情况下,它会被写出两倍或更多倍的冗余内容。

【问题讨论】:

  • 如果您遇到编译器错误,请将其添加到您的问题中。
  • 你为什么要把[i]放在所有的末尾?那是什么意思?请说明你想让x.surname.push_back(word)[i] 做什么而不是现在做什么。
  • 正确的包含是&lt;string&gt;
  • 您的问题是您认为std::vector&lt;std::string&gt; 的工作方式与std::vector&lt;int&gt; 不同。它们的工作方式相同,并且应该使用相同的方法。你使用amount(几乎)正确,以同样的方式使用surname
  • 请帮自己一个忙,获取a good C++ book。您不会通过尝试随机事物(例如x.surname(word)[i])来学习 C++。此外,迭代工作会有所帮助:您的程序有几个语法和逻辑错误。不要试图一次编写整个程序。写出有用的东西并扩展它们,一旦发现错误就修复它们。从输入和输出单个名称开始,然后输入和输出名称列表等。

标签: c++ string vector struct


【解决方案1】:

您将[i] 放在代码中看似随机的位置。比如x.surname.push_back(word)[i];。如果您不确定它们在做什么,请不要将此类内容添加到您的代码中。

x.surname(word)[i] 构造也是错误的。 x.surname(word) 应该是什么?此语法用于函数调用。但是,surname 不是函数。这是一个std::vector&lt;std::string&gt;。只需输入x.surname[i]

还有一件事。如何造句“没有姓氏!”成为 写一次?在某些情况下,它会被写出两次或更多次 什么是多余的。

那是因为您为每个不符合标准的捐赠者编写了它。相反,请跟踪是否有任何捐赠者符合标准,并且仅在没有最终符合标准时才打印。你可以这样做:

bool HasGorgeousDonators = false;

然后在循环中:

if (x.amount[i] >= 10000)
{
    cout << "Surname: " << x.surname[i];
    cout << "Amount: " << x.amount[i] << endl;
    HasGorgeousDonators = true;
}

在循环之后:

if (!HasGorgeousDonators)
    cout << "Lack of surnames!" << endl;

对于另一个循环也是如此。另外,请考虑以下问答:

Why is "using namespace std;" considered bad practice?

【讨论】:

    【解决方案2】:

    您似乎正在编写带有一些 C++ 帮助函数的 C。但是 C++ 是一种不同的语言。当然,它支持一些 C 结构,但还有更多。 看看我的一些实施建议并将其与您的代码进行比较:

    #include <string>
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    
    template<typename T>
    T ReadCin(std::string_view const& sv = "") {
        T retVal;
        if (!sv.empty()) std::cout << sv;
        std::cin >> retVal;
        return retVal;
    }
    
    class Donator {
    private:
        std::string surname{};
        int amount{};
    public:
        constexpr bool IsGenerous() const noexcept { return amount >= 10000; }
        void Read() noexcept {
            surname = ReadCin<decltype(surname)>("Surname: ");
            amount = ReadCin<decltype(amount)>("Amount: ");
        }
        friend std::ostream& operator<<(std::ostream& out, Donator const& donator) noexcept {
            out << "Surname: " << donator.surname << ", " << "Amount: " << donator.amount;
            return out;
        }
    };
    
    int main() {
        std::vector<Donator> donators(ReadCin<int>("How many donators do you want to register?\n"));
        for (auto& donator : donators) donator.Read();
    
        std::cout << "OUR GENEROUS DONATORS:\n";
        std::copy_if(std::cbegin(donators), std::cend(donators), std::ostream_iterator<Donator>(std::cout, "\n"),
            [](Donator const& donator) { return donator.IsGenerous(); });
    
        std::cout << "OUR CASUAL DONATORS:\n";
        for (auto const& donator : donators) if (!donator.IsGenerous()) std::cout << donator << '\n'; //alternative
    }
    

    我尝试包含一些使用 C++ 的可能性。我真的建议你买一本关于 C++ 的好书。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多