顺序容器的问题,assign,swap
下面的代码编译通不过,求指导
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
using namespace std;
int main() {
char *words[] = {"hello", "world", "hi"};
size_t word_size = sizeof(words) / sizeof(char *);
list<char *> lst(words, words+word_size);
vector<string> vect;
vect.assign(lst.begin(), lst.end());
return 0;
}
还有个人对swap()的理解,就是简单的把两个容器的名称对换了,不知道对不对? 顺序容器 assign swap
[解决办法]
没有编译不通过,你用得什么编译器
[解决办法]
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
using namespace std;
int main() {
char const *words[] = {"hello", "world", "hi"};
size_t word_size = sizeof(words) / sizeof(char *);
list<char const*> lst(words, words+word_size);
vector<string> vect;
vect.assign(lst.begin(), lst.end());
return 0;
}
[解决办法]
不对。 swap()调用发生在运行时, 而名称在编译后就不复存在。 swap需要交换两个容器的内部状态。
[解决办法]
VC6不支持其他类型的迭代器,只支持容器自己的迭代器
因为VC6不支持类的成员模板函数
换编译器吧
[解决办法]
让你用VC6!!!
------解决方案--------------------
lst.begin()的类型是list<char const*>::iterator,
vect自己的迭代器是vector<string>::iterator,两个类型不一样,VC6的话容器的区间构造用的迭代器必须是容器自己的迭代器类型