Click here to Skip to main content
15,888,071 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
C++
#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 file
C++
#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();
}
Posted
Comments
Sergey Alexandrovich Kryukov 18-Jan-16 19:25pm    
Why? Why not, for example, std::string? Anyway, this is not even a question.
What do you want to achieve, really?
—SA
Member 12223678 18-Jan-16 19:29pm    
the entire code implemented using vector
i want to change it to implement a pointer char* instead
barneyman 18-Jan-16 19:33pm    
no, no you don't - then you're messing with a container AND its contents, every time you add/del from it .... as SA says, use a std::string, or CString
Sergey Alexandrovich Kryukov 18-Jan-16 21:01pm    
This is totally illogical. Well, something is implemented this way, so what? 100 vectors in code makes the use of non-vectors impossible, or what?
If you don't explain your motivation or cannot, don't, but you hardly can expect some help.
—SA
[no name] 18-Jan-16 20:35pm    
What is the point to this exercise? I don't mean "i want to change it to implement a pointer char* instead".

1 solution

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
  • ...
In my opinion, the main disvantage of you own implmentation could be the lack of true reliability.

In any case if you really feel the urge of implementing yourself (a possibly specializaton of) the container, then the C++ language gives you the same tools it offers to the library coders, namely new/delete for memory allocation and the OOP infrastructure. Just read the documentation if you are not familiar with.
You have to focus on memory allocation/deallocation and on the implementation of methods similar to the ones of the std::vector you are currently using.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900