帮忙看下这个问题

xiaofang3100 2008-03-23 11:31:13
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void main()
{
char *words[]={"stately","plump","buck","mulligan"};
unsigned size=sizeof(words)/sizeof(char *);
vector<string> c(words,words+size);//这里出错,这种初始化出错了,我记得c++primer也是这么的,请帮回答下
for(vector<string>::iterator iter=c.begin();iter!=c.end();++iter)
cout<<*iter<<endl;
}

#include<iostream>
#include<string>
using namespace std;
void main()
{
char *cp="Hiya";
string s1(cp);
string s2("Hiydaaa");
string s3(s1,0);//这种初始化怎么出错了,c++primer跟我的一样,我也不知道怎么了
cout<<s3<<endl;
if(s1.compare(s2))
cout<<"yes";

}

这个问题主要是对于函数接口的传参,不怎么会用,请高手说明下哪出错了
#include<iostream.h>
typedef void (*print)(int);

void visit(int e)
{
cout<<e<<" ";
}

void ListTraverse(print,int n)
{
for(int i=0;i<n;++i)
print(i);
}


void main()
{
int n=10;
ListTraverse(visit,10);//为什么输出不了0-9
}

请帮忙回答下上面这3个问题,谢谢.
...全文
67 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
独孤过儿 2008-03-23
  • 打赏
  • 举报
回复
别给我分,问题是abupie兄答的,我只是无聊闲的,帮你调一下代码格式而已!
独孤过儿 2008-03-23
  • 打赏
  • 举报
回复

// 第一题没问题
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
char *words[] = {"stately", "plump", "buck", "mulligan"};
unsigned size = sizeof(words) / sizeof(char *);
vector <string> c(words, words+size);//这里出错,这种初始化出错了,我记得c++primer也是这么的,请帮回答下
for (vector <string>::iterator iter = c.begin(); iter != c.end(); ++iter)
cout << *iter << endl;
return 0;
}


// 第二题也没有问题
#include <iostream>
#include <string>
using namespace std;

int main()
{
char *cp = "Hiya";
string s1(cp);
string s2("Hiydaaa");
string s3(s1, 0);//这种初始化怎么出错了,c++primer跟我的一样,我也不知道怎么了
cout << s3 << endl;
if(s1.compare(s2))
cout << "yes";
return 0;
}



#include <iostream>
using namespace std;

/* 这样定义以后print是一个指向函数的指针类型,而不是一个指针变量 */
typedef void (*print)(int);

void visit(int e)
{
cout << e << " ";
}

void ListTraverse(print p,int n)
{
for (int i = 0; i < n; ++i)
(*p)(i); // 此处应该是指针变量,而不是指针类型
}

int main()
{
int n = 10;
ListTraverse(&visit, n);
return 0;
}

abupie 2008-03-23
  • 打赏
  • 举报
回复
3.第3个我改了下,输出0-9,楼主自己理解下吧。
另外,楼主太粗心了。比如#include <iostream.h> 应该是#include <iostream>, using namespace std;也漏了。
#include <iostream> 

using namespace std;

typedef void (*print)(int);

void visit(int e)
{
cout <<e <<" ";
}

void ListTraverse(print p,int n)
{
for(int i=0;i <n;++i)
(*p)(i);
}


int main()
{
int n=10;
ListTraverse(&visit,10);//ΪʲôÊä³ö²»ÁË0-9

system("pause");
return 0;
}
独孤过儿 2008-03-23
  • 打赏
  • 举报
回复
呵呵,空格不是他加的,是CSDN自动排版的时候加的...
abupie 2008-03-23
  • 打赏
  • 举报
回复
2 第2个也是正常的,楼主注意你输入的<< 中间有多余的空格,
#include <iostream> 
#include <string>
using namespace std;
int main()
{
char *cp="Hiya";
string s1(cp);
string s2("Hiydaaa");
string s3(s1,0);//ÕâÖÖ³õʼ»¯Ôõô³ö´íÁË,c++primer¸úÎÒµÄÒ»Ñù,ÎÒÒ²²»ÖªµÀÔõôÁË
cout << s3 <<endl;
if(s1.compare(s2))
cout <<"yes";

system("pause");
return 0;
}
abupie 2008-03-23
  • 打赏
  • 举报
回复
1.我用gcc编译执行都ok,没有出问题。
#include <iostream> 
#include <vector>
#include <string>
using namespace std;
int main()
{
char *words[]={"stately","plump","buck","mulligan"};
unsigned size=sizeof(words)/sizeof(char *);
vector <string> c(words,words+size);//ÕâÀï³ö´í,ÕâÖÖ³õʼ»¯³ö´íÁË,ÎҼǵÃc++primerÒ²ÊÇÕâôµÄ,Çë°ï»Ø´ðÏÂ
for(vector <string>::iterator iter=c.begin();iter!=c.end();++iter)
cout << *iter << endl;

system("pause");
return 0;
}
Supper_Jerry 2008-03-23
  • 打赏
  • 举报
回复
和硬件没有关系的。重新建个工程把代码拷上,看看能不能编译。
还是不行重装vc6.0。或者干脆换个编译器。
xiaofang3100 2008-03-23
  • 打赏
  • 举报
回复
我感觉是我在装vc6.0时候出问题了,我记得第一,二题在c++primer上就是这么写的,晕了
在我的机子上就是运行出错,不知道怎么了,有谁知道我机子硬件哪出问题了,还是vc6.0出问题了

64,849

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧