[英]How to gather different variable types from files C++
我正在用C ++編寫霍夫曼代碼,現在正在從文件中獲取數據,並且遇到了障礙。 第一行中有一個我想當作字符串的字符串,我一點也不覺得難。 但是然后我必須取下x個數字行並將其作為未簽名的字符,這樣我才能使用它們。
如果要提取此表單文件,我將輸入什么?
ILaILbILrILcLd(想作為字符串)
3(3是其下的#號)
89(想將這3個字符作為未簽名的字符取出
207
88
正如耐克告訴我們的那樣:
// read the line:
std::string line1;
std::getline(infile, line1);
// read the int:
int num;
infile >> num;
// probably want to sanity check `num` here
// define a place to store the chars
std::vector<char> chars;
// read the chars.
for (i=0; i<num; i++) {
unsigned int temp;
infile >> temp;
chars.push_back(static_cast<unsigned char>(temp));
}
在這種情況下,它可能不會完成很多工作,但是在定義了chars
,您可以添加: chars.reserve(num);
以避免重新分配vector
的內存。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.