-1

I wanted to have a linked list of nodes with below structure.

    struct node
    {
     string word;
     string color;
     node *next;
    }

for some reasons I decided to use vector instead of list.my question is that is it possible to implement a vector which it's j direction is bounded and in i direction is unlimited and to add more two strings at the end of my vertex. in other words is it possible to implement below structure in vector ?

            j       
    i   color1  color2  …
        word1   word2   …
vahidzolf
  • 109
  • 1
  • 1
  • 13
  • It's kind of difficult to understand this question, please rewrite it. Also add tag for a programming language (I guess C/C++, but just to be sure). – Viliam Búr Jan 10 '13 at 14:04
  • yeah your right this code is for c++ . I modified my answer a little for better understanding. – vahidzolf Jan 10 '13 at 14:10

1 Answers1

0

I am not good with C/C++, so this answer will only be very general. Unless you are extremely concerned about speed or memory optimization (most of the time you shouldn't be), use encapsulation.

Make a class. Make an interface which says what you want to do. Make the simples possible implementation of how to do it. Most of the time, the simplest implementation is good enough, unless it contains some bugs.

Let's start with the interface. You could have made it part of the question. To me it seems that you want a two-dimensional something-like-an-array of strings, where one dimension allows only values 0 and 1, and the other dimension allows any non-genative integers.

Just to make sure there is no misunderstanding: The bounded dimension is always size 2 (not at most 2), right? So we are basicly speaking about 2×N "rectangles" of strings.

What methods will you need? My guesses: A constructor for a new 2×0 size rectangle. A method to append a new pair of values, which increases the size of the rectangle from 2×N to 2×(N+1) and sets the two new values. A method which returns the current length of the rectangle (only the unbounded dimension, because the other one is constant). And a pair of random-access methods for reading or writing a single value by its coordinates. Is that all?

Let's write the interface (sorry, I am not good at C/C++, so this will be some C/Java/pseudocode hybrid).

class StringPairs {
    constructor StringPairs();  // creates an empty rectangle
    int size();  // returns the length of the unbounded dimension
    void append(string s0, string s1);  // adds two strings to the new J index
    string get(int i, int j);  // return the string at given coordinates
    void set(int i, int j, string s);  // sets the string at given coordinates
}

We should specify what will the functions "set" and "get" do, if the index is out of bounds. For simplicity, let's say that "set" will do nothing, and "get" will return null.

Now we have the question ready. Let's get to the answer.

I think the fastest way to write this class would be to simply use the existing C++ class for one-dimensional vector (I don't know what it is and how it is used, so I just assume that it exists, and will use some pseudocode; I will call it "StringVector") and do something like this:

class StringPairs {
    private StringVector _vector0;
    private StringVector _vector1;
    private int _size;

    constructor StringPairs() {
        _vector0 = new StringVector();
        _vector1 = new StringVector();
        _size = 0;
    }

    int size() {
        return _size;
    }

    void append(string s0, string s1) {
        _vector0.appens(s0);
        _vector1.appens(s1);
        _size++;
    }

    string get(int i, int j) {
        if (0 == i) return _vector0.get(j);
        if (1 == i) return _vector1.get(j);
        return null;
    }

    void set(int i, int j, string s) {
        if (0 == i) _vector0.set(j, s);
        if (1 == i) _vector1.set(j, s);
    }

}

Now, translate this pseudocode to C++, and add any new methods you need (it should be obvious how).

Using the existing classes to build your new classes can help you program faster. And if you later change your mind, you can change the implementation while keeping the interface.

Viliam Búr
  • 2,154
  • 17
  • 14
  • thankyou very much you issued the problem and then solve it. everything is fine except that I don't want to use two separate arrays to keep the strings . I want a two dimensional matrix so I can handle both string at the same time. – vahidzolf Jan 13 '13 at 11:31
  • @vahidzolf: You could handle both strings at the same time by making a "pair of strings" type, and then using "vector of (pair of strings)" for the matrix. Alternatively, you could just implement methods "void append(StringPair)", "void set(j, StringPair)" and "StringPair get(int j)" in this model. There are often more possible implementations for the same interface. Yet another solution, you could have *one* "vector of strings" wrapped in the object, and interpret its indices 0, 1, 2, 3, 4... as (0, 0), (1, 0), (0, 1), (1, 1), (2, 0)... respectively. Treat (0, J) as 2*J, and (1, J) as 2*J+1. – Viliam Búr Jan 14 '13 at 15:41
  • yeah that's possible and I talked to my boss and he suggested something like your first solution thanks. – vahidzolf Jan 22 '13 at 07:31