mar*_*arw 3 c++ vector wstring
我想知道,是否有人可以向我解释 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 );
Run Code Online (Sandbox Code Playgroud)
但是第二个论点呢?如何将 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;
}
Run Code Online (Sandbox Code Playgroud)
感谢您提供该问题的明确示例。这是一个修改后的版本,其中包含一些关于更改的评论。它在 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;
}
Run Code Online (Sandbox Code Playgroud)
理解插入调用
向量insert成员函数的原型是:
iterator insert( iterator pos, const T& value );
Run Code Online (Sandbox Code Playgroud)
哪里T是您作为模板参数提供的类型,即std::wstring在这种情况下。
迭代器已经operator+重载,语义如下:iterator it + integer 2返回一个新的迭代器,其位置比迭代器高 2 个“增量” it。
words.insert( it + position, word4 );
Run Code Online (Sandbox Code Playgroud)
建议
关于如何确定插入位置要小心的一件事。
我认为使用迭代器“沿着”向量“走”,而不是使用迭代器+偏移量会是更好的实践(更易于维护)。如果您对迭代器不是很满意,这将是一个学习如何使用它们的机会。
这将避免潜在的情况,在以前版本的这个答案,在这里你一不小心抵消你的迭代器的讨论过去的矢量结束,导致分段违例。