我正在此源代码:如何修改此标记化过程以在多行文本文件上工作?
#include <string>
#include <vector>
#include <iostream>
#include <istream>
#include <ostream>
#include <iterator>
#include <sstream>
#include <algorithm>
int main()
{
std::string str = "The quick brown fox";
// construct a stream from the string
std::stringstream strstr(str);
// use stream iterators to copy the stream to the vector as whitespace separated strings
std::istream_iterator<std::string> it(strstr);
std::istream_iterator<std::string> end;
std::vector<std::string> results(it, end);
// send the vector to stdout.
std::ostream_iterator<std::string> oit(std::cout);
std::copy(results.begin(), results.end(), oit);
}
器,代替标记化一行,并把它插入载体的结果,一个标记化组从这个文本文件所采取的行,并把所得到的字转换为一个单一的矢量。
Text File:
Munroe states there is no particular meaning to the name and it is simply a four-letter word without a phonetic pronunciation, something he describes as "a treasured and carefully-guarded point in the space of four-character strings." The subjects of the comics themselves vary. Some are statements on life and love (some love strips are simply art with poetry), and some are mathematical or scientific in-jokes.
到目前为止,我只清楚,我需要使用
while (getline(streamOfText, readTextLine)){}
得到循环运行。
但我不认为这会工作:
而(函数getline(streamOfText,readTextLine)){ COUT < < readTextLine < < ENDL;
//从字符串 的std :: stringstream的的strstr(readTextLine)构建体的流;
//使用流迭代器将流复制到向量中作为空白分隔字符串 std :: istream_iterator it(strstr); std :: istream_iterator end; std :: vector结果(it,end);
/*HOw CAN I MAKE THIS INSIDE THE LOOP WITHOUT RE-DECLARING AND USING THE CONSTRUCTORS FOR THE ITERATORS AND VECTOR? */
// send the vector to stdout.
std::ostream_iterator<std::string> oit(std::cout);
std::copy(results.begin(), results.end(), oit);
}
那么,有什么问题呢?你的解决方案对我来说很好。只需使用readTextLine作为stringstream构造函数的参数,并将所有代码包装到循环中即可。你面对什么问题? – 2009-01-27 21:42:31
包装循环中的代码..我应该在哪里放置它? 的std :: istream_iterator它(的strstr); std :: istream_iterator end; std :: vector results(it,end); –
andandandand
2009-01-27 21:48:11