假设我有一个以“:”作为分隔符的文本文件,我如何找到每列的平均值?对于例如第三列为(3 + 2 + 5)/ 3,第二列为(61 + 87)/ 2。从文本文件中找到平均值
我曾尝试在while循环中使用getline,但它似乎更复杂,因为我认为它需要比这更多。如果有人能够就此进行启发,我将不胜感激。谢谢!
Sample text file
================
3:290:61:100:
2:50:
5:346:87:
当前代码
void IDS::parseBase() {
string temp = "";
int counting = 0;
int maxEvent = 0;
int noOfLines = 0;
vector<string> baseVector;
ifstream readBaseFile("Base-Data.txt");
ifstream readBaseFileAgain("Base-Data.txt");
while (getline(readBaseFile, temp)) {
baseVector.push_back(temp);
}
readBaseFile.close();
//Fine the no. of lines
noOfLines = baseVector.size();
//Find the no. of events
for (int i=0; i<baseVector.size(); i++)
{
counting = count(baseVector[i].begin(), baseVector[i].end(), ':') - 1;
if (maxEvent < counting)
{
maxEvent = counting;
}
}
//Store individual events into array
string a[maxEvent];
while (getline(readBaseFileAgain, temp)) {
stringstream streamTemp(temp);
for (int i=0; i<maxEvent; i++)
{
getline(streamTemp, temp, ':');
a[i] += temp + "\n";
}
}
}
调试第一,以缩小你的问题,因为请问这里请! –
阅读一下关于如何计算stackoverflow的问题。这不是为了解决您的编程任务而提供的服务。答案应该有更多的一般应用,并且对很多人有用。 –