C++如何将 wstring 插入给定位置的矢量中

C++ how to insert wstring into vector at given position

本文关键字:位置 定位 wstring 插入 C++      更新时间:2023-10-16

我想知道,是否有人可以向我解释vector.insert()方法中的第二个参数:

迭代

器插入(迭代器位置,常量value_type和Val);

例如,我有一个 wstring 类型的向量,我想在给定位置插入一个 wstring。我已经弄清楚了如何使用迭代器设置位置:

wstring word = "test";
int insertion_pos = 3;
iterator it = words.begin();
words.insert( it + insertion_pos, word );

但是第二个论点呢?如何将 wstring 对象传递给 insert() 方法?多谢。

干杯

马丁

完整示例:

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <wchar.h>
#include <vector>
using namespace std;
int main(void) {
    // Initialize the vecor with three words.
    vector<wstring> words;
    wstring word1 = "FirstWord"; // Error msg: no viable conversion from 'const char     [10]' to 'wstring' (aka 
                             //            'basic_string<wchar_t>')
    wstring word2 = "SecondWord"; // same here
    wstring word3 = "ThirdWord"; // same here
    words.push_back(word1);
    words.push_back(word2);
    words.push_back(word3);
    // Now try to insert a new word at position 2 (i.e. between "SecondWord "and "ThirdWord"
    int position = 2;
    wstring word4 = "InsertThis"; // same error as above
    iterator it = words.begin(); // Error: use of class template iterator requires template 
                             //      arguments
    words.insert( it + position, word4 ); 
//  Invalid arguments ' Candidates are:     __gnu_cxx::__normal_iterator<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>> 
//   *,std::vector<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>,std::allocator<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>>>> 
//   insert(__gnu_cxx::__normal_iterator<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>> 
//   *,std::vector<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>,std::allocator<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>>>>, 
//   const std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>> &) void 
//   insert(__gnu_cxx::__normal_iterator<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>> 
//   *,std::vector<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>,std::allocator<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>>>>,     
//   unsigned long int, const std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>> &) void 
//   insert(__gnu_cxx::__normal_iterator<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>> 
//   *,std::vector<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>,std::allocator<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>>>>, 
//   #10000, #10000) '
    return EXIT_SUCCESS;
}

感谢您提供该问题的清晰示例。 这是一个修改后的版本,其中包含一些有关更改的评论。 它在Mac OS X上使用clang为我编译。

一个变化是字符串文本前面的"L"。 这是一个指示符,指示要遵循的字符串文本的类型为 wchar_t。 另请参阅此内容。

宽字符/unicode/utf 支持是我只有在您尝试解决的问题需要时才添加的东西。

// #include <stdio.h>   prefer "cstdio" to stdio.h; not used in example                                                                                                                                 
// #include <stdlib.h>  same                                                                                                                                                                            
#include <iostream>
#include <string>
// #include <wchar.h>  not used in example                                                                                                                                                              
#include <vector>
using namespace std;
// simplify to main()
int main() {
  // Initialize the vecor with three words.                                                                                                                                                             
  vector<wstring> words;
  wstring word1(L"FirstWord"); // Use Constructor, no assignment operator=                                                                                                                              
  wstring word2(L"SecondWord");
  wstring word3(L"ThirdWord");
  words.push_back(word1);
  words.push_back(word2);
  words.push_back(word3);
  int position = 2;
  wstring word4(L"InsertThis");
  // iterator depends on type of container                                                                                                    
  vector<wstring>::iterator it = words.begin();                                                                                                                                                         
  words.insert( it + position, word4 );
  for (const std::wstring& w : words)
    std::wcout << w << " ";
  std::wcout << std::endl;
  return EXIT_SUCCESS;
}

了解插入调用

向量的insert成员函数的原型为:

  iterator insert( iterator pos, const T& value );

其中T是您作为模板参数给出的类型,即 在这种情况下std::wstring

迭代器operator+重载,具有以下语义:iterator it + integer 2返回一个新的迭代器,其位置比迭代器it"增量"为 2 个。

  words.insert( it + position, word4 );

建议

关于

如何确定内行位置要小心的一件事。

我认为使用迭代器"

沿着"向量"行走"而不是使用迭代器+偏移量会更好(更易于维护)。 如果您对迭代器不是很熟悉,这将是一个学习如何使用它们的机会。

这将避免本答案的先前版本中讨论的潜在情况,即您不小心将迭代器偏移到向量末尾从而导致分割冲突。