C++字符串数组问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++字符串数组问题相关的知识,希望对你有一定的参考价值。
我想得到的结果是:
输入一个单词,依次获得并显示该单词的每一个字母,最后显示单词的字母数。
我写了下面的代码,但是不行。我很菜,望高手指点!希望多讲点字符串和数组部分的知识!
#include "iostream.h"
#include "string.h"
int main()
int i,j;
char *a=new char;
cout<<"输入一个单词:"<<endl;
cin>>a;
j=strlen(a);
for(i=0;i<j;i++)
cout<<a[i]<<" ";
delete []a;
return 0;
谢谢各位高手,下面有位朋友写出了代码,可是我最想要的是获取输入单词的字母数,就是说要得到输入的那个单词有多少个字母组成。能不能补充一下?谢谢了!
这一句有问题 你只是开辟了一个char型内存空间,这是不够存储字符串的。
可以直接char a[100];定义一个长度为100的字符串数组,注意char a[N]只能存N-1个字符,因为最后要有一个字符为'\0'表示结束 参考技术B #include <iostream>
#include <string>
using namespace std;
void main ()
char * p=new char[50];//char[20]是开辟20* 个char 空间。//你的就错在这,其他的都对了。
cin >> p;//输入单词。
cout << endl;
int count;
for (count=0;*(p+count)!='\0';count++)//字符串以'\0'结束。
cout << *(p+count) << " " ;
cout << "位数:" <<count <<endl;
delete []p;
本回答被提问者采纳 参考技术C char *a=new char[512];
在 C++ 中返回字符串数组
【中文标题】在 C++ 中返回字符串数组【英文标题】:return string arrays in c++ 【发布时间】:2018-10-05 23:00:51 【问题描述】:我是 c++ 新手,在尝试开发 go 之后,我想不妨学习 c++,因为当我开始深入抽象层时,学习曲线会非常陡峭,我习惯于在 php 中轻松处理动态数组我无法理解与 c++ 相关的指针、内存分配和范围问题,这是我的第一次尝试:
string* Deck()
// array of card rank and vlaue, seperated by a period, used as a delimiter for evaluting the players hands
string* Deck = new string[52] ;
Deck =
"A.H","2.H","3.H","4.H","5.H","6.H","7.H","8.H","9.H","10.H","J.H","Q.H","K.H",
"A.S","2.S","3.S","4.S","5.S","6.S","7.S","8.S","9.S","10.S","J.S","Q.S","K.S",
"A.D","2.D","3.D","4.D","5.D","6.D","7.D","8.D","9.D","10.D","J.D","Q.D","K.D",
"A.C","2.C","3.C","4.C","5.C","6.C","7.C","8.C","9.C","10.C","J.C","Q.C","K.C",
;
return Deck;
int main()
string* Deck = Deck();
cout << *Deck[23] << endl;
return 0;
编译器给了我各种错误,我尝试简单地返回指针,但我在这篇文章中发现了范围Return string array in C++ function
有很多关于 int
作为数据类型的文章,但 strings
和 int=>string
的解释很少见,因为我猜每个人都认为即使是菜鸟也能弄清楚,但我已经观看了无数小时的 youtube 视频,但仍然不能想办法 。谢谢。
【问题讨论】:
“我已经观看了无数小时的 youtube 视频”——这就是你出错的地方;你需要阅读一本好的 C++ 教科书。 “编译器给了我各种错误” - 你能发布一个错误,我们可以尝试帮助你解决它吗?我以前从未使用过 std::strings 数组,但我猜你只想要cout << Deck[23] << endl;
没有星号:这会给 cout 一个 std::string ,我认为它可以在没有任何进一步转换的情况下打印。 (我假设您并不是要取消引用指针以获取仅打印的第一个字符,如果这是一个 C 而不是 C++ 字符串的数组,会发生这种情况?)
ideone.com/e8DvzS
这段代码中的错误太多了。人们不只是对你直言不讳。你真的真的需要拿到一本书。你需要先掌握语言;然后您可以提出具体问题。
我也怀疑调用所有东西Deck
- 特别是同时具有变量和函数调用相同的东西。即使它按原样工作(我不确定它会不会),如果你给它们起不同的名字,读者会更清楚哪个 Deck 是哪个。
【参考方案1】:
" 我习惯于在 php 中轻松处理动态数组,但是我无法理解与 c++ 相关的指针、内存分配和范围问题"
在处理动态数组时,您无需像在PHP
中那样处理C++
中的“指针、内存分配和范围问题”。 p>
只需使用std::vector,它会为您管理数组:
std::vector<std::string> GetDeck()
// array of card rank and vlaue, seperated by a period, used as a delimiter for evaluting the players hands
std::vector<std::string> Deck =
"A.H","2.H","3.H","4.H","5.H","6.H","7.H","8.H","9.H","10.H","J.H","Q.H","K.H",
"A.S","2.S","3.S","4.S","5.S","6.S","7.S","8.S","9.S","10.S","J.S","Q.S","K.S",
"A.D","2.D","3.D","4.D","5.D","6.D","7.D","8.D","9.D","10.D","J.D","Q.D","K.D",
"A.C","2.C","3.C","4.C","5.C","6.C","7.C","8.C","9.C","10.C","J.C","Q.C","K.C",
;
return Deck;
int main()
auto Deck = GetDeck();
std::cout << Deck[23] << '\n';
【讨论】:
我支持这个。如果您看到任何鼓励您使用原始指针或上帝禁止手动内存管理的教程(原始new
\ delete
)知道这不是 C++ 应该使用的方式。以上是关于C++字符串数组问题的主要内容,如果未能解决你的问题,请参考以下文章