在标准输出上格式化矢量字符串
Format vector string on stdout
所以对于这个程序,我正在为类编写,我必须将向量字符串格式化为标准输出。我知道如何使用'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("%sn", it->c_str());
}
}
相关文章:
- 如何从C++中的格式化字符串派生整数?
- C++逐步格式化字符串
- C++:格式化包含十六进制值的字符串
- 格式化返回字符串C++
- 有没有更快的方法从成员函数返回格式化字符串
- 如何在C++中对静态缓冲区执行字符串格式化?
- 格式化的时间字符串在解析为time_point时会产生小时差异
- C++ ctime() 迄今为止格式化的字符串?
- 自定义 {fmt} 格式化函数,具有编译时格式字符串检查功能
- 如何在提升库中格式化字符串
- 使用参数的可变大小向量格式化字符串(例如,将参数向量传递给 std::snprintf)
- 使用 c++ 格式化字符串的自然方法
- 使用此字符串重新格式化器使用什么代替 substr
- snprintf等效于WCHAR_T计算格式化的字符串大小(MAC)
- WXSTRING:是否有使用类似Python3的占位符实现字符串格式化的C/C 代码
- C字符串.格式化插入斜杠
- 如何截断字符串 [格式化] ?C++
- 是否为osx标准库实现swscanf子字符串格式化([a-z])
- c++字符串格式化-固定宽度的浮点数,以及在偏移处有空格的字符串
- 如何创建可变模板字符串格式化程序