Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将字符串的c数组复制到std::string的向量中_C++_Vector - Fatal编程技术网

C++ 将字符串的c数组复制到std::string的向量中

C++ 将字符串的c数组复制到std::string的向量中,c++,vector,C++,Vector,我需要将字符串的c数组的元素存储在向量中 基本上,我需要将c数组的所有元素复制到向量中 #包括 #包括 #包括 使用名称空间std; int main() { char*a[3]={“field1”、“field2”、“field3”}; //这里有一些代码!!!! vector::const_迭代器it=fields.begin(); for(;it!=fields.end();it++) { coutstd::向量场(a,a+3); 您可以使用insert方法将内容添加到向量。从这里查看示例

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

基本上,我需要将c数组的所有元素复制到
向量中

#包括
#包括
#包括
使用名称空间std;
int main()
{
char*a[3]={“field1”、“field2”、“field3”};
//这里有一些代码!!!!
vector::const_迭代器it=fields.begin();
for(;it!=fields.end();it++)
{
cout
std::向量场(a,a+3);

您可以使用insert方法将内容添加到向量。从这里查看示例片段:
std::vector blah(a,a+数组的长度)
#包括
//#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
const char*a[3]={“field1”、“field2”、“field3”};
//如果你想创建一个全新的向量
向量v(a,a+3);
std::copy(v.begin(),v.end(),ostream_迭代器(cout,“\n”);
矢量v2;
//或者,如果已经有一个向量
向量(a,a+3)互换(v2);
std::copy(v2.begin(),v2.end(),ostream_迭代器(cout,“\n”);
向量v3;
v3.推回(“字段0”);
//或者,如果要向现有向量添加字符串
v3.insert(v3.end(),a,a+3);
std::copy(v3.begin(),v3.end(),ostream_迭代器(cout,“\n”);
}

它有两个地方是++。从其中一个地方删除++。一个包含字符串指针的c数组如何直接转换为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();
}
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);
std::vector<std::string> blah(a, a + LENGTH_OF_ARRAY)
#include<vector>
// #include<conio.h>
#include<iostream>
#include <iterator>
#include <algorithm>

using namespace std;

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

  // If you want to create a brand new vector
  vector<string> v(a, a+3);
  std::copy(v.begin(), v.end(), ostream_iterator<string>(cout, "\n"));

  vector<string> v2;
  // Or, if you already have an existing vector
  vector<string>(a,a+3).swap(v2);
  std::copy(v2.begin(), v2.end(), ostream_iterator<string>(cout, "\n"));

  vector<string> v3;
  v3.push_back("field0");
  // Or, if you want to add strings to an existing vector
  v3.insert(v3.end(), a, a+3);
  std::copy(v3.begin(), v3.end(), ostream_iterator<string>(cout, "\n"));

}