我需要将c数组字符串的元素存储在向量中。将c字符串数组复制到std :: string向量中
基本上我需要将c数组的所有元素复制到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();
}
任何人都可以帮我把c数组元素存储到一个向量中吗?
编辑
这下面的代码被倾倒的核心!!请帮助
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();
}
你拥有了它++在两个地方。从其中之一删除++。 – Dialecticus 2011-06-10 14:23:36
是的,你是对的。感谢:) – Vijay 2011-06-10 14:26:53