将字符串的c数组复制到std :: string的向量中 [英] copying c array of strings into vector of std::string

查看:102
本文介绍了将字符串的c数组复制到std :: string的向量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在向量中存储字符串数组的元素。



基本上,我需要将ac数组的所有元素复制到 vector< std :: string>

 #include< 
#include< conio.h>
#include< iostream>

using namespace std;

int main()
{
char * a [3] = {field1,field2,field3};

//这里的代码!

vector< std :: string> :: const_iterator it = fields.begin();
for(; it!= fields.end(); it ++)
{
cout << * it ++<< endl;
}
getch();
}

任何人都可以帮助我将c数组元素存储到向量中? p>

EDIT



这下面的代码是倾销核心! >

  int main()
{
char * a [3] = {field1,field2 field3};
std :: vector< std :: string>字段(a,a + 3);

vector< std :: string> :: const_iterator it = fields.begin();
for(; it!= fields.end(); it ++)
{
cout << * it ++<< endl;
}
getch();
}


解决方案

  std :: vector< std :: string>字段(a,a + 3); 


I need to store elements of a c array of strings in a vector.

Basically I need to copy all the elements of a c array into a vector<std::string>.

#include<vector>
#include<conio.h>
#include<iostream>

using namespace std;

int main()
{
    char *a[3]={"field1","field2","field3"};

    //Some code here!!!!

    vector<std::string>::const_iterator it=fields.begin();
    for(;it!=fields.end();it++)
    {
        cout<<*it++<<endl;
    }   
    getch();
}

Could anybody help me out to store c array elements into a vector?

EDIT

this below code is dumping the core!!Pls help

int main()
{
    char *a[3]={"field1","field2","field3"};
    std::vector<std::string> fields(a, a + 3);

    vector<std::string>::const_iterator it=fields.begin();
    for(;it!=fields.end();it++)
    {
        cout<<*it++<<endl;
    }   
    getch();
}

解决方案

std::vector<std::string> fields(a, a + 3);

这篇关于将字符串的c数组复制到std :: string的向量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆