How to save a list of sets of three lines?

std::vector<std::pair<std::string > > 

can be used to store a list of a couple of lines. Is there a similar way to store a list of string triplets?

One way I can think of is to use std::vector

 std::vector<std::vector<std::string > > v (4, std::vector<std::string> (3)); 

but this will not allow me to use manual first and second accessors.

So I wrote my own class

 #include <iostream> #include <vector> using namespace std; template <class T> class triad { private: T* one; T* two; T* three; public: triad() { one = two = three =0; } triad(triad& t) { one = new T(t.get1()); two = new T(t.get2()); three = new T(t.get3()); } ~triad() { delete one; delete two; delete three; one = 0; two = 0; three = 0; } T& get1() { return *one; } T& get2() { return *two; } T& get3() { return *three; } }; int main() { std::vector< triad<std::string> > v; } 

I have two questions

  • Is there any way to improve class code?
  • Is there a better way to store string triplets than the three methods described above?
+4
source share
4 answers

std::tuple is a generalization of std::pair that allows you to store a collection of a certain size. So you can say:

 typedef std::tuple<std::string, std::string, std::string> Triad; 

to get a Triad type that stores three lines.

+9
source

You can use tuples from boost: http://www.boost.org/doc/libs/1_49_0/libs/tuple/doc/tuple_users_guide.html

(In C ++, 11 tuples are part of the standard library: http://en.wikipedia.org/wiki/C%2B%2B11#Tuple_types )

+5
source

As already mentioned, you should use std::tuple (or boost::tuple ). However, I think there are things you can learn from criticizing your code:

  • If you want something like std::pair , why don't you write something that behaved like std::pair ? That is, a class with public data elements that copies its values ​​by value?
  • If you want something like std::pair , why not make it more general, like std::pair , where different members can have different types? Or if you only care about std::string member storage, why bother creating a template class?
  • Your copy constructor should accept its argument using the const reference.
  • You have not fulfilled rule rule 3. You have a non-trivial copy constructor and a non-trivial destructor, but you neglect the implementation of the copy assignment operator. However, if you made No. 1, you will not need to worry about it.
  • As indicated, it is not possible to set class data members. The constructor cannot initialize them with the specified values, and after constructing them, it is impossible to set them. (If you try triad.get1() = ... , you will encounter an attempt to dereference a null pointer!)
  • Your constructors should initialize their data members using initialization lists. For instance:

    triad() : one(0), two(0), three(0) { }

  • One of the things that makes std::pair useful for simply creating the struct desired members is that it comes with operator== and operator< .

  • If you really want to use access methods, you should provide const versions.
+3
source

If your triad is specific, then create a class or at least a structure so that you can give meaningful names to the members.

 struct Contact { std::string name; std::string nickname; std::string email; }; std::vector<Contact> contacts; 
+1
source

Source: https://habr.com/ru/post/1403025/


All Articles