【发布时间】:2017-11-26 12:32:30
【问题描述】:
我的任务是计算数组中的许多“a”字母
这是我的代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int i;
int ch[10];
cout<<"enter 10 letters"<<endl;
for(i=0;i<10;i++){
cin>>ch[i];
}
cout<<endl;cout<<endl;
cout<<"this is the letter you entered"<<endl;
cout<<endl;
for(i=0;i<10;i++){
cout<<(ch[i])<<endl;
}
vector<int> a = ch[10];
int cnt;
cnt = count(a.begin(), a.end(), "a");
cout<<"many a's are = "<<cnt<<endl;
}
但它给了我错误 [错误] 请求从“int”转换为非标量类型“std::vector”
请帮帮我, 从https://www.tutorialspoint.com/cpp_standard_library/cpp_algorithm_count.htm引用我的代码
【问题讨论】:
-
试试
std::copy(std::begin(ch),std::end(ch),std::begin(a));而不是vector<int> a = ch[10];。 -
表达式
ch[10]并不代表整个数组。另外,区分int和char,以及字符文字和字符串文字。 -
vector<int> a = ch[10];本身有 2 个错误:1) 不存在从int(ch数组的第 11 个元素)到std::vector<int>的转换 2) 您正在引用外部的元素数组的边界 - 调用未定义的行为。我建议你阅读good C++ book。 -
数组名是
ch,不是ch[10],"a"是字符数组,不是字符。 -
不不不,我的意思是,我会通过 cin 将一个字符 a 插入到数组中,也是一个变量