实现矢量的char * insted [英] implementing char* insted of vector
本文介绍了实现矢量的char * insted的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好大家请查看我的代码我试图取一串数字然后压缩它
之前在.hpp我声明了一个字符向量(向量< char> nume)到存储新的压缩数字字符串
现在您可以看到我将其更改为char *和int size
你能推荐一个新的类实现吗?新的字母*和大小而不是矢量
.hpp文件
hello everyone please check out my code i'm trying to take a string of numbers and compress it
before in the .hpp i declared a vector of chars (vector<char> nume) to store the new string of compressed numbers
and now as you can see i changed it to a char* and int size
can you recommend a new implementation of the class using this new char* and size instead of a vector
.hpp file
#include "iostream"
using namespace std;
#include "string"
#include "vector"
class Mint{
public:
Mint();
Mint(int);
Mint(const char*);
bool operator<(const Mint&);
bool operator>(const Mint&);
void afficher();
void size();
private:
char* num;
int size;
};
.cpp文件
.cpp file
#include "Mint.h"
#include "string.h"
Mint::Mint()
{
taille = 1;
num = new char[taille];
*num = '\0';
}
Mint::Mint(const char* s)
{
if(strlen(s)%2)
num.push_back(s[0]-'0');
unsigned int i;
for(i=strlen(s)%2;i<strlen(s);i+=2)
{
int left = s[i] - '0';
int right = s[i+1]-'0';
num.push_back(0);
num.back() = left << 4;
num.back() |= right;
}
}
bool Mint::operator<(const Mint& rhs)
{
unsigned int i;
if (num.size()<rhs.num.size())
return true;
if (num.size()==rhs.num.size())
for(i=0;i<num.size();i++)
{
if(num[i] < rhs.num[i])
return true;
}
return false;
}
bool Mint::operator>(const Mint& rhs)
{
if (*this < rhs)
return false;
return true;
}
void Mint::afficher()
{
unsigned int i;
for (i=0;i<num.size();i++)
{
int first_digit = (num[i] & '\xF0')>>4;
int second_digit = (num[i] & '\x0F');
if (i || first_digit) cout << first_digit;
cout << second_digit;
}
}
void Mint::size()
{
cout << num.size();
}
推荐答案
正如其他人所发现的,制作标准库容器的版本通常没有意义,因为容器写得很好,就是它们是
- 可靠
- 高效
- 有严格的界面
- ......
As others spotted, it is usually pointless to make you own version of a standard library container, because such containers are well written, that is they are
- Reliable
- Efficient
- Have a rigorous interface
- ...
这篇关于实现矢量的char * insted的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文