首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

vector 输出有关问题

2013-08-01 
vector 输出问题#include iostream#include fstream#include stdio.h#include string.h#include

vector 输出问题
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;

int main(){
//读取myfile文件
char a[256];
ifstream infile;
infile.open("d:\\myfile.txt");
infile>>a;
infile.close();



//拆分字符串
const char *d=";,.";
char *p;
p=strtok(a,d);
vector<string> v;
while(p){
//printf("%s\n",p);
v.push_back(p);
p=strtok(NULL,d);
};

for(int j = 0; j < v.size(); ++j){
        cout << v[j] << endl;
    }
return 0;
}

它在倒数第4行  cout << v[j] << endl;报了个C2679的错,说是 二进制“<<”: 没有找到接受“std::basic_string<_Elem,_Traits,_Ax>”类型的右操作数的运算符(或没有可接受的转换)

怎么改呢
[解决办法]

for(int j = 0; j < v.size(); ++j){
         cout << v[j] << endl;
     }
//改为:
for(int j = 0; j < v.size(); ++j){
         cout << v[j].data() << endl;
     }

因为<<是重载操作符,相当于函数传入的参数不对,让你传入int ,你传入了 char *
[解决办法]
引用:
Quote: 引用:

Quote: 引用:

这个已经没有问题了,现在我怎么获得vector中每一个元素的大小?
什么叫做每个元素的大小?sizeof?还是字符串长度?


每个字符串长度~~~


v[j].length()

热点排行