English 中文(简体)
C++ 接触病媒
原标题:Accessing Vectors In C++
  • 时间:2011-01-26 06:19:10
  •  标签:
  • c++
  • vector

我写了一个简单的方案,可以查阅一个称为“投入.txt”的档案,将其内容推向一种扼杀性媒介。

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

int main()
{
    fstream input;
    input.open("input.txt");
    string s;
    input >> s;
    while (!input.eof())
    {
        cout << s <<endl;
        input >> s;
        vector<string> v;
        v.push_back(s);
    }
    input.close();

}

我知道,这是这样做的最佳途径,但我是否能够利用这一媒介,并把它用于一个方程式? 如果我想要把病媒的所有内容加在一起并找到这些内容?

最佳回答

对开端人来说,这部法律的更清洁版本是你刚才写的:

ifstream input("input.txt");
string s;
while (input >> s) {
    cout << s << endl;
    vector<string> elems;
    elems.push_back(s);
}

This uses ifstream instead of fstream, which seems appropriate here since you aren t using the write facilities of fstream. It also combines the read logic with the loop condition, simplifying the logic for when to continue.

似乎与这一守则相矛盾的是,你将<条码>查询<>/代码>的范围扩大,使之仅能为一种选择而生活。 因此,每张缩略语中,你就失去了所有旧的<条码> 查询/编码>内容。 摆脱这种循环,或许可以确定:

ifstream input("input.txt");
vector<string> elems;

string s;
while (input >> s) {
    cout << s << endl;
    elems.push_back(s);
}

最后,如果你想要接手<条码>查询/编码>的内容。 将这些文件加在一起,你可能不想把档案视为示意图,而是作为<条码>、<条码>或<条码>。 这使你不必在以后转换价值。 例如:

ifstream input("input.txt");
vector<double> elems;

double s;
while (input >> s) {
    cout << s << endl;
    elems.push_back(s);
}

现在,你可以共同增加所有价值观:

double total = 0.0;
for (int i = 0; i < elems.size(); ++i)
    total += elems[i];

或者,更糟的是,你可以使用<代码>accumulate把所有东西加在一起。 <代码><数和>;:

double total = accumulate(elems.begin(), elems.end(), 0.0);

希望这一帮助!

问题回答

你的方案是不正确的。 或许你指的是:

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

int main()
{
    ifstream input("input.txt");
    string s;
    vector<string> v;
    while (input >> s)
    {
        cout << s <<endl;
        v.push_back(s);
    }
}

此外,这里还有另一种做法:

#include<algorithm>
#include<iterator>
#include<fstream>
#include<string>

struct ShowAndCopy {
    const std::string& operator()(const std::string& s) const {
        std::cout << s;
        return s;
    }
};

int main()
{
    std::ifstream input("input.txt");
    std::vector<std::string> v;
    std::transform(std::istream_iterator<std::string>(input),
                   std::istream_iterator<std::string>(),
                   std::back_inserter(v),
                   ShowAndCopy());
    return 0;
}

是的,你可以反复分析扼杀媒介的内容,将价值转换为愤怒或浮动点价值,并加以总结。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签