在主要功能有一行這條線是如何在這個C++代碼片段中工作的?
vector<bookStatusEnum> booksStatus(numbooks, available);
你能解釋一下這行被執行?我看到代碼段中沒有booksStatus函數。所以它究竟是什麼。
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
struct book
{
string title, author;
inline bool operator<(const book& other) const
{
if (author != other.author)
return author < other.author;
return title < other.title;
}
};
void ReadInBooks(vector<book> & allBooks)
{
string temp;
book currentBook;
while (cin >> temp, temp != "END")
{
currentBook.title = "";
bool first = true;
while (*(temp.end() - 1) != '"')
{
if (!first)
currentBook.title += ' ';
first = false;
currentBook.title += temp;
cin >> temp;
}
if (!first)
currentBook.title += ' ';
currentBook.title += temp;
// Ignore 'by'
cin >> temp;
// ignore the space after the by
cin.ignore();
getline(cin, currentBook.author);
allBooks.push_back(currentBook);
}
}
enum bookStatusEnum { available, borrowed, returned };
int main()
{
vector<book> allBooks;
ReadInBooks(allBooks);
int numBooks = allBooks.size();
std::sort(allBooks.begin(), allBooks.end());
vector<bookStatusEnum> booksStatus(numBooks, available);
map<string, int> positionInAllBooks;
for (int i = 0; i < numBooks; ++i)
positionInAllBooks[allBooks[i].title] = i;
vector<bool> borrowed(allBooks.size(), false);
vector<bool> returned(allBooks.size(), false);
string command;
string bookName;
while (cin >> command, command != "END")
{
if (command == "BORROW")
{
cin.ignore();
getline(cin, bookName);
int pos = positionInAllBooks[bookName];
borrowed[pos] = true;
returned[pos] = false;
}
else if (command == "RETURN")
{
cin.ignore();
getline(cin, bookName);
returned[positionInAllBooks[bookName]] = true;
}
else // SHELVE
{
int previous = -1;
for (int i = 0; i < numBooks; ++i)
{
if (!borrowed[i])
previous = i;
else if (returned[i])
{
if (previous == -1)
cout << "Put " << allBooks[i].title << " first\n";
else
cout << "Put " << allBooks[i].title << " after " << allBooks[previous].title << '\n';
returned[i] = false;
borrowed[i] = false;
previous = i;
}
}
cout << "END\n";
}
}
}
P.S:我沒有找到合適的標題。請隨時提出建議。
是不是隻是向量的構造函數參數? – apokryfos
它可以像'vector booksStatus {numbooks,available};' –
user5821508
那樣重新編寫,該行只是說創建一個大小爲'numbooks'並初始化爲'available'的枚舉向量。矢量的名稱將爲'booksStatus' – fersarr