我目前正在尝试将数字文本文件解析为2d向量,稍后将对其进行修改,但是到目前为止,我的代码都是如此。我得到这个:
> flag <- c('PICU', 'CCCU')
> df %>%
+ group_by(key) %>%
+ mutate(ICU = Patient_Unit %in% flag)
# A tibble: 21 x 3
# Groups: key [7]
key Patient_Unit ICU
<int> <chr> <lgl>
1 1 7A FALSE
2 2 2B FALSE
3 3 CCCU TRUE
4 4 PICU TRUE
5 5 7A FALSE
6 6 2B FALSE
7 7 CCCU TRUE
8 1 PICU TRUE
9 2 7A FALSE
10 3 2B FALSE
# ... with 11 more rows
一切正常,除了重复第9行并在末尾添加垃圾内容。
如果我在文本的末尾输入一个回车,它将显示以下内容:
845630179
032918654
190745328
683074912
457201836
219863540
361429705
074186093
074186093�845630179
(第9行中的第10个元素不应该在那里)
供参考,以下是文本文件的外观:
845630179
032918654
190745328
683074912
457201836
219863540
361429705
074186093
9203574619
到目前为止,这是我的代码:
845630179
032918654
190745328
683074912
457201836
219863540
361429705
074186093
920357461
答案 0 :(得分:0)
您的代码对我来说效果很好。
如果文本文件末尾有回车,它将最后一个数字也推送到网格中。否则,您需要在while循环之后将temp
的内容推送到grid
。
答案 1 :(得分:0)
一个快速解决方案可以解决您的大部分问题:
int main()
{
//parsing the textfile.
vector<vector<char>> grid;
fstream fin;
char ch;
// Hardcoded value. Typing the same thing in over and over while debugging is for suckers.
string name("data.txt"); //File Name.
// 2D Vector.
vector<char> temp;
// Temporary vector to be pushed
// into vec, since its a vector of vectors.
fin.open(name.c_str(), ios::in);
// Assume name as an arbitary file.
while (fin.get(ch)) // the change: Slightly different get and getting in the loop
// condition. If nothing is gotten the loop doesn't enter
// solves almost all of the problems.
{
if (ch != '\n')
{
temp.push_back(ch);
cout << ch;
}
else
{
grid.push_back(temp);
temp.clear();
cout << ch;
}
}
for (int i = 0; i < grid.size(); i++)
{
for (int j = 0; j < grid[i].size(); j++)
{
cout << grid[i][j];
}
}
}
输出
845630179 032918654 190745328 683074912 457201836 219863540 361429705 074186093 920357461845630179032918654190745328683074912457201836219863540361429705074186093
最后将垃圾丢掉了。那不是垃圾。那就是您的实际输出。一切
845630179 032918654 190745328 683074912 457201836 219863540 361429705 074186093 920357461
是进行收集的while循环中cout << ch;
s的结果。
845630179032918654190745328683074912457201836219863540361429705074186093
最后是for
循环。由于未存储换行符,因此将其打印为一大块数字。
为解决此问题,我们将从while
循环中删除输出,并在for
循环中恢复换行。
int main()
{
//parsing the textfile.
vector<vector<char>> grid;
fstream fin;
char ch;
string name("data.txt"); //File Name.
// 2D Vector.
vector<char> temp;
// Temporary vector to be pushed
// into vec, since its a vector of vectors.
fin.open(name.c_str(), ios::in);
// Assume name as an arbitary file.
while (fin.get(ch))
{
if (ch != '\n')
{
temp.push_back(ch);
}
else
{
grid.push_back(temp);
temp.clear();
}
}
for (int i = 0; i < grid.size(); i++)
{
for (int j = 0; j < grid[i].size(); j++)
{
cout << grid[i][j];
}
cout << '\n'; //
}
}
如果我们使用vector<string>
执行此操作,则大约四分之一的代码将消失。
int main()
{
//parsing the textfile.
vector<string> grid;
fstream fin;
// 2D Vector.
vector<char> temp;
// Temporary vector to be pushed
// into vec, since its a vector of vectors.
fin.open("data.txt", ios::in);
// Assume name as an arbitary file.
string line;
while (getline (fin, line))
{
grid.push_back(line);
temp.clear();
}
for (int i = 0; i < grid.size(); i++)
{
for (int j = 0; j < grid[i].size(); j++)
{
cout << grid[i][j];
}
cout << '\n'; // getline ate the newline. Have to put it back
}
}