3

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

迭代器插入(迭代器位置,const 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;
}
4

1 回答 1

5

感谢您提供该问题的明确示例。这是一个修改后的版本,其中包含一些关于更改的评论。它在 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返回一个新的迭代器,其位置超过迭代器 2“增量” it

  words.insert( it + position, word4 );

建议

关于如何确定插入位置要小心的一件事。

我认为使用迭代器“沿着”向量而不是使用迭代器+偏移量会是更好的做法(更易于维护)。如果您对迭代器不是很满意,这将是一个学习如何使用它们的机会。

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

于 2013-11-02T22:30:04.790 回答