在线等 致向数组的指针 如何遍历 关系全家老小吃饭问题 帮忙啊

Eniak 2013-11-07 04:46:26
有这样一个数组,知道指针,想把传到一个函数里面,然后进行遍历,对方就给出了头函数,让我实现,帮忙啊

想不出来怎么办, 求啊,关系全家老小吃饭问题




const char * strs[] = {
"ape", "apes", "apse", "asp", "pas", "pase", "pea",
"peas", "pes", "sae", "sap", "sea", "spa", "spae"
};

Boggle A;
A.solve(strs);





void Boggle::Solve( const char * a_Grid) const
{
在这里把他们一个一个的打出来 a_Grid 里面的东东
}
...全文
207 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2013-11-09
  • 打赏
  • 举报
回复
引用 14 楼 Vegertar 的回复:
[quote=引用 12 楼 lm_whales 的回复:] [quote=引用 11 楼 Vegertar 的回复:] 用模板推导长度呗

template <int N>
void solve(const char *(&strs)[N])
{
  for (int i = 0; i < N; ++i) {
    cout << strs[i] << endl; 
  }
}

好吧,可以这么处理,不过千万不要,在另一个文件定义const char * strs[],然后 extern const char * strs[]; 也不要把 const char * strs[]={。。。}; 写在使用solve 的函数下面。 [/quote] 编译时处理的模板推导,能编译成功自然不会有越界的情况发生,你说的情况从语法上就已经断绝了。:)[/quote] 问题是,有时不可能编译成功。 因为,可能会引用库内定义的外部变量。 或者,不同文件内引用变量。 靠编译成功与否,来解决这个问题,并不一定是恰当的。 所以,必须自己确定,是否适合使用这个模板。
Vegertar 2013-11-08
  • 打赏
  • 举报
回复
引用 12 楼 lm_whales 的回复:
[quote=引用 11 楼 Vegertar 的回复:] 用模板推导长度呗

template <int N>
void solve(const char *(&strs)[N])
{
  for (int i = 0; i < N; ++i) {
    cout << strs[i] << endl; 
  }
}

好吧,可以这么处理,不过千万不要,在另一个文件定义const char * strs[],然后 extern const char * strs[]; 也不要把 const char * strs[]={。。。}; 写在使用solve 的函数下面。 [/quote] 编译时处理的模板推导,能编译成功自然不会有越界的情况发生,你说的情况从语法上就已经断绝了。:)
电工小菜鸟 2013-11-08
  • 打赏
  • 举报
回复
void sol(const char * a_Grid[]) { int i =0; while (a_Grid[i]!=NULL) { cout<<a_Grid[i]<<" "; i++; } }
lm_whales 2013-11-08
  • 打赏
  • 举报
回复
引用 11 楼 Vegertar 的回复:
用模板推导长度呗

template <int N>
void solve(const char *(&strs)[N])
{
  for (int i = 0; i < N; ++i) {
    cout << strs[i] << endl; 
  }
}

好吧,可以这么处理,不过千万不要,在另一个文件定义const char * strs[],然后 extern const char * strs[]; 也不要把 const char * strs[]={。。。}; 写在使用solve 的函数下面。
Vegertar 2013-11-07
  • 打赏
  • 举报
回复
用模板推导长度呗

template <int N>
void solve(const char *(&strs)[N])
{
  for (int i = 0; i < N; ++i) {
    cout << strs[i] << endl; 
  }
}

super_admi 2013-11-07
  • 打赏
  • 举报
回复
其实,只要楼主研究研究.ini文件的读取,就应该知道怎么搞这玩意了。 不管你的函数参数是否传错,这里肯定是用两个'\0'作为整体结束符,一个'\0'作为单个字符串的结束符。所以:

char* strs;
...
do {
while(*strs){
//do something.
strs++;
};
strs++;
}while(*strs);
还有多远 2013-11-07
  • 打赏
  • 举报
回复
lz标题不对哦

const char * strs[] = {
    "ape", "apes", "apse", "asp", "pas", "pase", "pea",
    "peas", "pes", "sae", "sap", "sea", "spa", "spae"
};//strs[]是常量char型指针的数组,即数组中的每一个元素都指向一个常量字符串
lm_whales 2013-11-07
  • 打赏
  • 举报
回复
可以这么做:
const char * strs[] = {
	"ape", "apes", "apse", "asp", "pas", "pase", "pea",
	"peas", "pes", "sae", "sap", "sea", "spa", "spae",NULL //加一个NULL表示结束,可以用0
};

Boggle A;
A.solve(strs);


void Boggle::Solve( const char ** a_Grid) const
{
assert(a_Grid);//断言指针非空,传入空指针,调试出错。
if(!a_Grid)return ;//保证指针非空。 
  //在这里把他们一个一个的打出来, a_Grid 里面的东东。
   while(*a_Gird){
    std::cout<<*a_Gird++ <<std::endl;
   }
}
Eniak 2013-11-07
  • 打赏
  • 举报
回复
再问一下, 把它转成 string 怎么做啊,机器还在报错啊


	while (a_Grid)
	{
		string word( (*a_Grid)[0],&(a_Grid)[strlen(*a_Grid)]);
		a_Grid++;
		this->T->Insert_A_Word(word);
	}
max_min_ 2013-11-07
  • 打赏
  • 举报
回复
直接是打印不了的 一个是cons char *[] 一个是const char * 要么循环调用A.solve(strs[i]) //i循环 要么修改函数原型!
baichi4141 2013-11-07
  • 打赏
  • 举报
回复
函数参数错误 应该是Solve( const char ** a_Grid, const int Num )
Eniak 2013-11-07
  • 打赏
  • 举报
回复
不知道啊,是不是错了,我觉着也不对啊
版主大哥 2013-11-07
  • 打赏
  • 举报
回复
关系全家老小吃饭问题 这么严重,小伙伴们...赶紧滴
arthursaber 2013-11-07
  • 打赏
  • 举报
回复
参数应该是char* a_Grid[]或者是char** a_Grid吧,就char * a_Grid输入的只是单个字符串不是串组吧 while (a_Grid) { printf("%s", a_Grid++); }
纯洁的老黄瓜 2013-11-07
  • 打赏
  • 举报
回复
const char** 指针付给const char*?好像会报错哦

64,281

社区成员

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

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