c++STL值assign

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/kaikai_sk/article/details/54428932
#include <iostream>
#include <list>
#include <vector>
#include <string>
using namespace std;

int main()
{
    char* ca[] = { "i","really","love","you" };
    list<char*> clist(ca, ca + 4);
    list<char*>::iterator list_iter=clist.begin();

    vector<string> s_vec;
    vector<string>::iterator vec_iter = s_vec.begin();

    s_vec.assign(clist.begin(), clist.end());

    if (!s_vec.empty())
    {
        for (int i = 0; i < s_vec.size(); i++)
        {
            cout << s_vec[i] << " ";
        }
        cout << endl;

        /*
        Expression: vector iterators incompatible
        for (; vec_iter != s_vec.end(); vec_iter++)
        {
            cout << *vec_iter<<" ";
        }
        cout << endl;*/
    }
    else
    {
        cout << "s_vec is empty" << endl;
        while (list_iter != clist.end())
        {
            cout << *list_iter << " ";
        }
        cout << endl;
    }
}
展开阅读全文

VC支持STLassign吗?

02-28

[size=14px]各位前辈好!小生最近在学习《C++ Primer》第四版。今天做到P284第9.28习题,遇到不解之处望不吝赐教。rn题目是:编写程序将一个list容器的所有元素赋值给一个vector容器,其中list容器中存储的是指向C风格字符串的char*指针,而vector容器的元素则是string类型。rn问题是:我使用VC6.0使用assign函数将list中的元素复制到vector容器中去,可就是出错误,似乎VC6.0中的assgin函数和书上讲的不一样。请各前辈指教!rnrn我写的代码是:[/size]rn#include "stdafx.h"rn#include rn#include rn#include rn#include rnusing namespace std;rnrnint main( )rnrn //为复制做准备rn char* str[ ] = "zhang", "huang", "qian", "yang", "cheng";rn list cplist(str, str + 5);rn vector svector;rnrn //复制开始rn svector.assign(cplist.begin( ), cplist.end( ));rnrnrn //输出验证rn cout << "list容器中的元素是:";rn for (list::iterator lit = cplist.begin( ); lit != cplist.end( ); ++lit)rn cout << *lit << " ";rn cout << endlrn << "vector容器中的元素是:";rn for (vector::iterator vit = svector.begin( ); vit != svector.end( ); ++vit)rn cout << *vit << " ";rn cout << endl;rn rn return 0;rnrnrn错误提示是:rnerror C2664: 'void __thiscall std::vector,class std::allocator >,class std::allocatorrn,class std::allocator > > >::assign(const class std::basic_string,class std::allocator > *,const class std::basic_string,class std::allocator > *)' : cannot convrnert parameter 1 from 'class std::list >::iterator' to 'const class std::basic_string,class std::allocator > *'rn No user-defined-conversion operator available that can perform this conversion, or the operator cannot be calledrn执行 cl.exe 时出错.rnrn9_28.exe - 1 error(s), 0 warning(s) 论坛

没有更多推荐了,返回首页