C++ printf std::vector

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2500109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:42:54  来源:igfitidea点击:

C++ printf std::vector

c++vectorprintf

提问by Sebtm

How I can do something like this in C++:

我如何在 C++ 中做这样的事情:

void my_print(format_string) {
   vector<string> data;
   //Fills vector
   printf(format_string, data);
}

my_print("%1$s - %2$s - %3$s");
my_print("%3$s - %2$s);

I have not explained well before. The format string is entered by the application user.

我之前没有解释清楚。格式字符串由应用程序用户输入。

In C# this works:

在 C# 中,这有效:

void my_print(format_string) {
 List<string> data = new List<string>();
 //Fills list
 Console.WriteLine(format_string, data.ToArray);
}

my_print("{0} - {1} - {2}");
my_print("{2} - {1}");

采纳答案by Sebtm

I have temporarly solved with this function:

我暂时解决了这个功能:

string format_vector(string format, vector<string> &items)
{   
   int counter = 1;

   replace_string(format,"\n","\n");
   replace_string(format,"\t","\t");

   for(vector<string>::iterator it = items.begin(); it != items.end(); ++it) {
        ostringstream stm; stm << counter;
        replace_string(format, "%" + stm.str(), *it);
        counter++;
   }
    return format;
}

回答by John Dibling

If you're going to use streams, you can also use ostream_iteratorin conjunction with a looping construct like copy:

如果您打算使用流,您还可以ostream_iterator与循环结构结合使用,例如copy

vector<string> data;
data.assign(10, "hello");

copy( &data[0], &data[3], ostream_iterator<string>(cout, " "));

Note that the second parameter to copypoints to one past the end. Output:

请注意,第二个参数 tocopy指向最后一个。输出:

hello hello hello

你好你好你好

回答by John Dibling

printf("%s - %s - %s", data[0].c_str(), data[1].c_str(), data[2].c_str() );

Note that you must convert to C-style strings - printf cannot do this for you.

请注意,您必须转换为 C 样式的字符串 - printf 无法为您执行此操作。

Edit:In response to your revised question, I think you will have to parse the format string yourself, as you will have to validate it. printf() won't do the job.

编辑:针对您修改后的问题,我认为您必须自己解析格式字符串,因为您必须对其进行验证。printf() 不会完成这项工作。

回答by Ken Bloom

The Boost Format Librarymight be helpful.

升压格式库可能会有所帮助。

#include <boost/format.hpp>
#include <vector>
#include <string>
#include <iostream>
int main(int arc, char** argv){
   std::vector<std::string> array;
   array.push_back("Hello");
   array.push_back("word");
   array.push_back("Hello");
   array.push_back("again");
   boost::format f("%s, %s! %s %s! \n");
   f.exceptions( f.exceptions() &
     ~ ( boost::io::too_many_args_bit | boost::io::too_few_args_bit )  );

   for (std::vector<std::string>::iterator i=array.begin();i!=array.end();++i){
      f = f % (*i);
   }
   std::cout << f;
   return 0;
}

回答by Ken Bloom

I think you're looking to do the following:

我认为您希望执行以下操作:

  1. Convert your std::vector<std::string>into a va_listof char*s
  2. Pass that va_list, along with the user-supplied format string to vprintf.
  1. 将您的转换std::vector<std::string>为 a va_listof char*s
  2. 将那个va_list以及用户提供的格式字符串传递给vprintf.

I still don't know how to do step 1. (What I do know is that most higher-level languages, such as Java, Scala, and Ruby have a simple, safe, direct conversion for that. C++ doesn't.)

我仍然不知道如何执行第 1 步。(我所知道的是大多数高级语言,例如 Java、Scala 和 Ruby,都有一个简单、安全、直接的转换。C++ 没有。)

回答by wheaties

Call the ones you want

打电话给你想要的人

printf("%1$s - %2$s - %3$s", date[0].c_str(), data[1].c_str(), data[2].c_str());