2013-06-02 43 views
0

因此,对于我正在为类编写的程序,我必须将矢量字符串格式化为标准输出。我知道如何用'printf'函数的字符串来完成它,但我不明白如何使用它来完成它。在标准输出上格式化矢量字符串

这里就是我的了:

void put(vector<string> ngram){ 
while(!(cin.eof())){ ///experimental. trying to read text in string then output in stdout. 
printf(ngram, i);/// 
+0

不要你的意思'printf'? – Dave

+0

是的,让我解决这个问题。 – user2421178

+0

你打算做什么?如果你只是想将矢量字符串格式化为标准输出,为什么你需要while(!(cin.eof()))? – billz

回答

0

好吧,我不读了很多你的问题,但是从我的理解,要打印字符串矢量到标准输出!?这可以这样工作:

void put(std::vector<std::string> ngram){ 
    for(int i=0; i<ngram.size(); i++) 
    { 
     //for each element in ngram do: 
     //here you have multiple options: 
     //I prefer std::cout like this: 
     std::cout<<ngram.at(i)<<std::endl; 
     //or if you want to use printf: 
     printf(ngram.at(i).c_str()); 
    } 
    //done... 
    return; 
} 

这就是你想要的吗?

+0

是的!对不起,如果我很直率地解释我正在尝试做什么。我对此很新。感谢您的帮助。 – user2421178

0

如果你只是想在一行中的每个项目:

void put(const std::vector<std::string> &ngram) { 

    // Use an iterator to go over each item in the vector and print it. 
    for (std::vector<std::string>::iterator it = ngram.begin(), end = ngram.end(); it != end; ++it) { 

     // It is an iterator that can be used to access each string in the vector. 
     // The std::string c_str() method is used to get a c-style character array that printf() can use. 
     printf("%s\n", it->c_str()); 

    } 

}