如何从文件中获取整数并将它们放入 int 向量中?

发布于 2024-12-08 11:54:55 字数 1758 浏览 1 评论 0原文

我正在尝试获取一个文本文件并获取文件内的整数并将它们传输到一个可以读入不同函数的向量中。

这是我到目前为止所拥有的:

int main(int argc, char *argv[] )
{
vector<int> buff;
argv[1] = "input_24_0.txt";

if (argc < 2)
{
    std::cout << "usage: " << argv[0] << " <filename>\n";
    return 2;
}
std::ifstream fin(argv[1]);
if (fin)
{
    std::stringstream ss;
    // this copies the entire contents of the file into the string stream
    ss << fin.rdbuf();
    // get the string out of the string stream
    std::string contents = ss.str();
    std::cout << contents;
    // construct the vector from the string.
    std::vector<int> buff(contents.begin(), contents.end());
}
else 
{
    std::cout << "Couldn't open " << argv[1] << "\n";
    return 1;
}

clock_t t1, t2, t3, t4;

int maxSum;

t1 = clock();
maxSum = maxSubSum1(buff);
cout << "MaxSubSum1 is " <<  maxSum << endl;
cout << double( clock() - t1 )
/ (double)CLOCKS_PER_SEC<< " seconds." << endl;
t1 = clock() - t1;

t2 = clock();
maxSum = maxSubSum2( buff );
cout << "MaxSubSum2 is " <<  maxSum << endl;
cout << double( clock() - t2)
/ (double)CLOCKS_PER_SEC<< " seconds." << endl;
t2 = clock() - t2;

t3 = clock();
maxSum = maxSubSum3(buff );
cout << "MaxSubSum3 is " <<  maxSum << endl;
cout << double( clock() - t3 )
/ (double)CLOCKS_PER_SEC<< " seconds." << endl;
t3 = clock() - t3;

t4 = clock();
maxSum = maxSubSum4( buff );
cout << "MaxSubSum4 is " <<  maxSum << endl;
cout << double( clock() - t4 )
/ (double)CLOCKS_PER_SEC<< " seconds." << endl;
t4 = clock() - t4;

system("pause");

return 0;
 }

I am trying to take a text file and take the integers inside the file and transfer them into a vector in which can be read into different functions.

This is what i have so far:

int main(int argc, char *argv[] )
{
vector<int> buff;
argv[1] = "input_24_0.txt";

if (argc < 2)
{
    std::cout << "usage: " << argv[0] << " <filename>\n";
    return 2;
}
std::ifstream fin(argv[1]);
if (fin)
{
    std::stringstream ss;
    // this copies the entire contents of the file into the string stream
    ss << fin.rdbuf();
    // get the string out of the string stream
    std::string contents = ss.str();
    std::cout << contents;
    // construct the vector from the string.
    std::vector<int> buff(contents.begin(), contents.end());
}
else 
{
    std::cout << "Couldn't open " << argv[1] << "\n";
    return 1;
}

clock_t t1, t2, t3, t4;

int maxSum;

t1 = clock();
maxSum = maxSubSum1(buff);
cout << "MaxSubSum1 is " <<  maxSum << endl;
cout << double( clock() - t1 )
/ (double)CLOCKS_PER_SEC<< " seconds." << endl;
t1 = clock() - t1;

t2 = clock();
maxSum = maxSubSum2( buff );
cout << "MaxSubSum2 is " <<  maxSum << endl;
cout << double( clock() - t2)
/ (double)CLOCKS_PER_SEC<< " seconds." << endl;
t2 = clock() - t2;

t3 = clock();
maxSum = maxSubSum3(buff );
cout << "MaxSubSum3 is " <<  maxSum << endl;
cout << double( clock() - t3 )
/ (double)CLOCKS_PER_SEC<< " seconds." << endl;
t3 = clock() - t3;

t4 = clock();
maxSum = maxSubSum4( buff );
cout << "MaxSubSum4 is " <<  maxSum << endl;
cout << double( clock() - t4 )
/ (double)CLOCKS_PER_SEC<< " seconds." << endl;
t4 = clock() - t4;

system("pause");

return 0;
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅听莫相离 2024-12-15 11:54:55

如果文件中的数字由空格分隔,您可以执行以下操作:

 std::ifstream iStream;
 iStream.open("filename.ext");
 int tempVariable;

 if (iStream) {
   while (iStream >> tempVariable) {
      // Any routines to check the value of the number if necessary
      vector.push_back(tempVariable);
   }
   iStream.close();
 }
 else
   // Error routine

如果数字不由空格分隔,您可以使用字符串流方法,并循环遍历流,将每个 char 转换为它是十进制值。

If the numbers in the file are separated by whitespace, you could do the following:

 std::ifstream iStream;
 iStream.open("filename.ext");
 int tempVariable;

 if (iStream) {
   while (iStream >> tempVariable) {
      // Any routines to check the value of the number if necessary
      vector.push_back(tempVariable);
   }
   iStream.close();
 }
 else
   // Error routine

If the numbers are not separated by whitespace, you can use your string stream approach, and loop through the stream converting each char into it's decimal value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文