增量数组
Increment Array
本文关键字:数组 更新时间:2023-10-16
每当字母'a'
出现在用户输入中时,我都试图增加数组(alphabet[0]
)索引,但当我打印出alphabet[0]
时,我收到了错误的输出。
示例问题:
"Enter a string"
adam //input
2665453 //printed on screen
2665453
2665453
2665453
2665453
我试图实现的输出应该是数字2,对于adam中的2'a。
这是我的代码:
class Counter {
public:
string input; //the string used for user input
int alphabet[26];
int numbers[10];
void countcharacters();
void countnumbers();
private:
};
void Counter::countcharacters() {
cout << "Enter a string" <<endl;
getline(cin, input);
for(int i=0; i<input.length(); i++) {
if(input.at(i) == 'a'){
alphabet[0]++;
}
cout << alphabet[0] << endl;
}
}
问题很简单。您的内存未初始化。数组中包含值不正确的int。在计数器类的构造函数中,将它们全部设置为0。
使用std::fill可以很容易地做到这一点,它可以在算法头中找到。有关它的文档可以在这里找到。
class Counter{
public:
Counter()
{
std::fill(std::begin(alphabet), std::end(alphabet), 0); // C++11
std::fill(alphabet, alphabet + 10, 0); // c++03
}
// The rest of your class goes here
};
std::begin
和std::end
模板可以在迭代器头中找到。
这里还有一个控制输入的例子,并存储它计数的字符的每个计数:
////////////////////////////
// AlphaNumCounter.h
////////////////////////////
#ifndef ALPHANUMCOUNTER_H
#define ALPHANUMCOUNTER_H
#include "stdafx.h"
#include <vector>
#include <map>
namespace MyStuff
{
const int NOT_VALID_SEARCH_CHAR = -1;
class AlphaNumCounter
{
public:
AlphaNumCounter();
AlphaNumCounter(bool bIsCaseSensitive);
unsigned int CountOccurance(const std::string &haystack, const char &needle);
// Debug func that print all the occurances of a character on the last CountOccurance call
void PrintOccurances()
{
for( std::map<char, unsigned int>::iterator pIter = m_mapAlphaNumerics.begin(); pIter != m_mapAlphaNumerics.end(); pIter++)
{
char c = (*pIter).first;
int count = (*pIter).second;
std::cout << "'" << c << "' had '" << count << "' occurances on last count iteration." << std::endl;
}
}
private:
void Init(); // Shared initializer for all ctor's
std::map<char, unsigned int> m_mapAlphaNumerics; // A map which holds the number of occurances of char (i.e: ['a'][7], if 'a' was found 7 times )
bool m_bIsCaseSensitive; // Should 'A' be counted as 'a', and vice versa...?
};
}
#endif // ALPHANUMCOUNTER_H
/////////////////////////////////
// AlphaNumCounter.cpp
/////////////////////////////////
#include "stdafx.h"
#include "AlphaNumCounter.h"
#include <algorithm>
using namespace MyStuff;
AlphaNumCounter::AlphaNumCounter() : m_mapAlphaNumerics(), m_bIsCaseSensitive(false)
{
Init();
}
AlphaNumCounter::AlphaNumCounter(bool bIsCaseSensitive) : m_mapAlphaNumerics(), m_bIsCaseSensitive(bIsCaseSensitive)
{
Init();
}
void AlphaNumCounter::Init()
{
// Store lowercase a - z
for( char i = 'a'; i <= 'z'; i++ )
m_mapAlphaNumerics[i] = 0;
// Store uppercase a-z
for( char i = 'A'; i <= 'Z'; i++ )
m_mapAlphaNumerics[i] = 0;
// Store 0 - 9
for( char i = '0'; i <= '9'; i++ )
m_mapAlphaNumerics[i] = 0;
}
unsigned int AlphaNumCounter::CountOccurance(const std::string &haystack, const char &needle)
{
// If neither a-z || A-Z || 0-9 is being searched for, we return a indication of that.
if(m_mapAlphaNumerics.find(needle) == m_mapAlphaNumerics.end())
return NOT_VALID_SEARCH_CHAR;
char ch = needle;
std::string sz = haystack;
// If the check is not case sensitive (i.e: A == a), we make sure both params are lowercase for matching.
if( !m_bIsCaseSensitive ){
ch = ::tolower(ch);
std::transform(sz.begin(), sz.end(), sz.begin(), ::tolower);
}
// Count occurances of 'ch' in 'sz' ('needle' in 'haystack')
std::size_t n = std::count(sz.begin(), sz.end(), ch);
// The occurances of 'needle' must be set to 'n' for this iteration
m_mapAlphaNumerics[ch] = n;
// Also set the uppercase val if its case insensitive. Then both 'a' and 'A' would have the same occurance count.
if( !m_bIsCaseSensitive )
m_mapAlphaNumerics[::toupper(ch)] = n;
return n; // Return the count for convenience of usage
}
//////////////////////
// Main.cpp
//////////////////////
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
MyStuff::AlphaNumCounter aCounter;
aCounter.PrintOccurances();
cout << "n--------------------------------------------------------------n";
cout << "Enter a string: " << std::endl;
string haystack = string();
getline(cin, haystack);
cout << "nNow enter a single alphanumerical character to count, in that string: " << endl;
char needle;
cin >> needle;
cin.clear();
cin.ignore( numeric_limits<streamsize> ::max(), 'n');
while( aCounter.CountOccurance(haystack, needle) == MyStuff::NOT_VALID_SEARCH_CHAR )
{
cout << "nI repeat: enter a single *alphanumerical* character to count, in that string: " << endl;
cin >> needle;
cin.clear();
cin.ignore( numeric_limits<streamsize> ::max(), 'n');
}
cout << "n--------------------------------------------------------------n";
aCounter.PrintOccurances();
system("PAUSE");
return 0;
}
希望它能有所帮助!当做Oyvind
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '