【问题标题】:How to append c-style string or string literals to std::vector<char*>?如何将 c 样式的字符串或字符串文字附加到 std::vector<char*>?
【发布时间】:2020-09-29 11:44:22
【问题描述】:

我正在尝试将 字符串文字 附加到 std::vector&lt;char*&gt; 但无论我尝试什么都无法附加它们。

std::vector<char*> vector1;

vector1.push_back("examplestring"); // gives warning C++11 does not allow conversion form string literal..

const char* anotherstring = "examplestring"; 
vector1.push_back(anotherstring);   // gives error no matchcing member to call push_back

有人可以用正确的方式来纠正我在向量中添加字符串的方法吗? 提前致谢。

【问题讨论】:

  • 为什么你首先有一个std::vector&lt;char*&gt;?为什么不是std::vector&lt;std::string&gt;
  • 你的向量有char *s。在 C++ 中,不能将 const char * 分配给 char *,也不能将 const char *s 添加到向量中。这就是 C++ 的工作原理。
  • @user123445151156:我敢问你为什么会故意在新代码中使用已弃用的安全相关函数?!?
  • @user123445151156 您可以存储std::vector&lt;std::string&gt; vector1 并将元素作为vector1[i].data() 传递给外部函数。
  • @user123445151156 函数对传递给它的字符串有什么作用?正确的方法实际上取决于该问题的答案。

标签: c++ string vector


【解决方案1】:

在您的代码 sn-p 中,没有 std::string 类型的对象。您编写的 sn-p 代码处理字符串文字。

C++ 中的字符串字面量具有常量字符数组的类型。

例如,此调用中使用的字符串文字

vector1.push_back("examplestring");

无法从const char[14] 类型转换为char * 类型。

如果你打算在向量中只存储指向字符串字面量的指针,那么你应该像这样声明向量

std::vector<const char *> vector1;

否则您需要动态分配字符串并将它们推送到向量上。例如

#include <cstring>
#include <vector>

//...

std::vector<char *> vector1;

char *s = new char[ sizeof( "examplestring" )];
std::strcpy( s, "examplestring" );
vector1.push_back( s );

当不再需要向量时,您必须释放分配的内存以存储字符串。

如果您想存储std::string 类型的对象,则必须使用动态内存分配创建对象的副本。否则字符串的生命周期至少应该不小于向量的生命周期。

例如

std::vector<char *> vector1;
std::string str( "examplestring" );

char *s = new char[ str.size() + 1];
std::strcpy( s, str.c_str() );
vector1.push_back( s );

【讨论】:

    【解决方案2】:

    您可以使用const_cast 抛弃const-ness,以便在接口期望非常量对象的情况下传递常量对象。但是请注意,这只是为了桥接接口;但是,您将(强制转换的)常量值(或访问所传递对象的任何其他部分)传递给的函数不得更改该对象。这将产生未定义的行为:

    std::vector<char*> vector1;
    
    const char* anotherstring = "examplestring";
    char* anotherStringNonConst = const_cast<char*>(anotherstring);
    
    vector1.push_back(anotherStringNonConst);
    

    【讨论】:

    • 这解决了我的问题。非常感谢@john,他在 cmets 中提到了相同的解决方案。
    • 不安全。尝试修改字符串文字会导致未定义的行为
    • @userbb 所以...如果函数 doesn't 实际上尝试修改指向字符串文字的char*,则没有UB ...明确的答案声明这仅适用于可以确定(通过参考源代码或询问开发人员)该功能不会这样做的情况。如果满足,就不可能有任何UB。
    • 同意。回答说,双手干净。但是为什么我们不能完全确定地分配内存。
    【解决方案3】:
    #include <cstdio>
    //#include <string>
    #include <vector>
    
    int main() {
        std::vector<const char*> vector1;
    
        vector1.push_back("examplestring");
        const char* anotherstring = "examplestring";
        vector1.push_back(anotherstring);
    
        for (auto* pCstring : vector1) {
            printf("%s\n", pCstring);
        }
    
        return 0;
    }
    

    使用std::vector&lt;const char*&gt; 有效,但仍强烈建议使用std::vector&lt;std::string&gt; 以确保内存安全。

    【讨论】:

      【解决方案4】:

      如何将std::string 附加到std::vector&lt;char*&gt;

      例子:

      std::string example;
      std::vector<char*> vector1;
      vector1.push_back(example.data());
      

      请注意,您必须小心保持example 至少在使用指针时保持活动状态。并且您不得以使指针无效的方式修改字符串。

      【讨论】:

      • 嗯,这可能是个问题,因为这个解决方案没有给我匹配的成员函数来调用回推
      • @user123445151156 哎呀,我忘了。您需要使用data() 来获取指向非常量的指针。
      • 再次没有运气,它适用于您的情况吗?也许我做错了什么
      • @user123445151156 如果您使用古老的编译器,它将无法工作。在这种情况下,您可以投射指针。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      • 2012-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-04
      相关资源
      最近更新 更多