如何从 C++ 中的结构数组打印所有内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14695805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:37:03  来源:igfitidea点击:

How to print everything from an struct array in c++?

c++struct

提问by paddy

I have a structured a data type called bookStruct and books is the name of the variable associated with the bookStruct data type. book[10] is the array that is 10 characters long and has 4 characters of data, meaning book[0] to book [3] have datas in them when the rest are empty (o values). Now I want to print the datas that are availabe already in the array and not print the ones that are empty otherwise 0. I tried the below code, with no luck.What am I doing wrong here?

我有一个名为 bookStruct 的结构化数据类型,books 是与 bookStruct 数据类型关联的变量的名称。book[10] 是长度为 10 个字符的数组,有 4 个字符的数据,这意味着 book[0] 到 book [3] 在其余为空时(o 值)中有数据。现在我想打印数组中已经可用的数据,而不打印那些为空的数据,否则为 0。我尝试了下面的代码,但没有运气。我在这里做错了什么?

for (int i=0;i<MAX_BOOKS && books[i]!='
struct bookStruct
{
    string bookTitle;
    int bookPageN;
    int bookReview;
    float bookPrice;
};

bookStruct books[10];
';i++) { cout << "Book Title: " << books[i].bookTitle << endl; cout << "Total Pages: " << books[i].bookPageN << endl; cout << "Book Review: " << books[i].bookReview << endl; cout << "Book Price: " << books[i].bookPrice<< "\n\n" << endl; }

here is the declaration for book struct

这是 book struct 的声明

struct Book {
    string title;
    int pageN;
    int review;
    float price;
};

ostream& operator<<(ostream& os, const Book& book) {
    return os << "Title: " << book.title << endl
              << "Pages: " << book.pageN << endl
              << "Review: " << book.review << endl
              << "Price:" << book.price << endl;
}

回答by Peter Wood

Overload the output operator. E.g:

重载输出运算符。例如:

Book book;
ostringstream oss;
oss << book;

Then you can output to any stream:

然后你可以输出到任何流:

bool bookHasTitle(const Book& book) {
    return book.title.empty() == false;
}

Book books[10];

copy_if(books, books+10, ostream_iterator<Book>(cout, "\n"),
        bookHasTitle);

To loop over all your books, copying to std::cout, only when a book has a title, you need std::copy_if:

要循环遍历所有书籍,复制到std::cout,仅当书籍具有标题时,您需要std::copy_if

int numBooks = 0;

回答by paddy

It's a little hard to tell what's being asked here. You see, it's quite easy to just keep count of how many books you have stored:

很难说出这里问的是什么。你看,计算你存储了多少本书是很容易的:

for( int i=0; i<numBooks; i++ ) ...

When you add a book, you increment numBooksup until MAX_BOOKS.

当您添加一本书时,您会递增numBooks直到MAX_BOOKS

for( int i=0; i<MAX_BOOKS && !books[i].bookTitle.empty(); i++ ) ...

If you don't want to do that, you certainly can't test books[i] != '\0'because that is testing the struct against a single character.

如果你不想这样做,你当然不能测试,books[i] != '\0'因为这是针对单个字符测试结构。

Instead, you might want to test books[i].bookTitle.size() != 0

相反,您可能想要测试 books[i].bookTitle.size() != 0

(or indeed !books[i].bookTitle.empty()).

(或确实!books[i].bookTitle.empty())。

vector<bookStruct> books;

...

for( int i = 0; i < books.size(); i++ ) ...

Another alternative is to store books in a vectorinstead of an array, so you don't have to worry about maximum counts and current counts. The vector can shrink and grow for you.

另一种选择是将书籍存储在一个vector而不是数组中,因此您不必担心最大计数和当前计数。矢量可以为您缩小和增长。

##代码##