因此,对于我正在为课程编写的这个程序,我必须将向量字符串格式化为标准输出。我知道如何使用带有'printf'函数的字符串来做到这一点,但我不明白如何做到这一点。
这是我得到的:
void put(vector<string> ngram){
while(!(cin.eof())){ ///experimental. trying to read text in string then output in stdout.
printf(ngram, i);///
好的,我无法从您的问题中阅读很多内容,但据我了解,您想将字符串向量打印到标准输出!?这将像这样工作:
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;
}
那是你想要的吗?
如果您只想将每个项目放在一行中:
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());
}
}