【问题标题】:How to pass string and vector into system()?如何将字符串和向量传递给 system()?
【发布时间】:2017-08-11 12:59:22
【问题描述】:

我一直在尝试将向量传递给system() 函数。 我已经阅读过有关此主题的先前问题,但我空手而归。

class aloha
{
private:
    string str;

public:
   void Caller() {
   std::string line;
   std::vector<string>vl;

   std::ifstream file("data.txt");
   while (file >> line) {
       vl.push_back(line);
       std::string c = "Taskkill /F /IM " (vl.begin(), vl.end())));
       system(c.c_str()));
       }
    }
};

我收到以下错误,这让我抓狂:

||=== Build: Debug in Instruction (compiler: GNU GCC Compiler) ===|
error: expression cannot be used as a function

error: cannot convert 'std::basic_string<_CharT, _Traits,
_Alloc>::c_str<char, std::char_traits<char>, std::allocator<char> >' from type 'const char* (std::basic_string<char>::)() const' to type
'const char*'|`

提前致谢,如果你们需要更多信息/示例代码,我会提供更多。

抱歉,我是 C++ 新手

【问题讨论】:

  • 可能你没仔细看……反正没有string::c_st,你的意思应该是string::c_str()
  • string c = "Taskkill /F /IM " (vl.begin(), vl.end()))); 应该做什么?
  • 顺便说一句,要么我遗漏了某些东西,要么您的错误消息与代码不匹配。我怀疑在你的真实代码中你有 c.c_str 在这里你有 c.c_st
  • @JohannesKhazaal 这不是连接字符串向量的方式。为此,请参阅:stackoverflow.com/questions/1985978/…

标签: c++ string vector system


【解决方案1】:

下面的行是要查看的行。

string c = "Taskkill /F /IM " (vl.begin(), vl.end())));

你可以用 std::append 连接字符串 http://www.cplusplus.com/reference/string/string/append/

您需要在 for 循环中执行此操作,一次附加一个向量的每个项目。但是,由于示例中的向量仅包含一个元素,因此上面的代码并不完全需要。一旦你有了你想要的字符串,你就可以将它传递给 system()。

【讨论】:

  • 也可以使用operator+=进行拼接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-16
  • 1970-01-01
  • 2016-01-17
相关资源
最近更新 更多