2

Is it possible to use operator<< to push strings into a vector. I searched a lot but only find stream examples.

class CStringData
{
 
    vector< string > myData;
    // ...
    // inline  operator << ... ???
};

I want this to use as a simple elipsis (like void AddData(...) ) exchange for robust parameters.

CStringData abc;
abc << "Hello" << "World";

Is this possible at all ?

KcFnMi
  • 5,516
  • 10
  • 62
  • 136
howieh
  • 107
  • 8
  • Why do you *want* to have `operator<<` push `string`s into a `vector`? – Nicol Bolas Dec 07 '11 at 07:29
  • @NicolBolas: Because it seems very useful, and handy. You can insert many strings in just one line! – Nawaz Dec 07 '11 at 07:37
  • 1
    @Nawaz: You can do that by putting a "`; `" between each `object.push_back()` call too. Doing it in a single expression doesn't buy you anything. And it makes the code more obtuse, because it looks like stream output. Which it very much is not. – Nicol Bolas Dec 07 '11 at 07:46
  • @NicolBolas: What you're suggesting requires too much keyboard hits. Also, if it looks like stream output, *because* you're accustomed to see *only* `stream <<`. Those who have not seen `<<` for stream output, might think it is doing left-shift operation, which it very much is not. – Nawaz Dec 07 '11 at 07:59
  • 'What you're suggesting requires too much keyboard hits.' Sounds like something a pointy haired would say, not a programmer... http://www.dilbert.com/ – jv42 Dec 07 '11 at 08:50
  • @jv42: So according to you, programmers' job is to write a verbose code, even when it is not required from any point of view? – Nawaz Dec 07 '11 at 08:59
  • @Nawaz You can write very verbose code with very *few* keyboard hits. Copy/paste, IntelliSense, snippets, etc... all allow to use very few keyboard hits to produce heaps of code lines. I just meant that 'hitting the keyboard' is not a very good metric of code quality. – jv42 Dec 07 '11 at 09:45
  • In the present case, I don't see using `<<` in a non standard way (hence reducing code readability, and increasing bug risks) as a progress over a series of push_back(), or, gloups, a variadic method. – jv42 Dec 07 '11 at 09:47
  • @jv42: I thought you would understanding that by "keyboard hits", I meant verbose code. – Nawaz Dec 07 '11 at 09:48
  • @jv42: As for this, "*In the present case, I don't see using << in a non standard way*". It sounds more like a religious argument, rather than rational one. – Nawaz Dec 07 '11 at 09:50
  • @Nawaz There are very good reasons for using standards. It allows confidence while reading the code, that it does what it looks like it does. In the current case, it might not seem very bad (and indeed, I think most decent C++ developers would get it), but still, overloading operators should be done very carefully so they behave as expected, even if it requires a bit more code in the end. – jv42 Dec 07 '11 at 10:24
  • @jv42: I would say it is matter of documentation and consistency. As I said before (in response toNicolBolas's comment), those who have not seen `<<` for stream output (as the standard library does), might think it is doing left-shift operation, which it very much is not. – Nawaz Dec 07 '11 at 10:29

4 Answers4

8

You can define operator<< as:

class CStringData
{
    vector< string > myData;
  public:
    CStringData & operator<<(std::string const &s)
    {
         myData.push_back(s);
         return *this;
    }
};

Now you can write this:

CStringData abc;
abc << "Hello" << "World"; //both string went to myData!

But instead of making it member function, I would suggest you to make it friend of CStringData:

class CStringData
{
    vector< string > myData;

  public:
    friend  CStringData & operator<<(CStringData &wrapper, std::string const &s);
};

//definition!
CStringData & operator<<(CStringData &wrapper, std::string const &s)
{
     wrapper.myData.push_back(s);
     return wrapper;
}

The usage will be same as before!

To explore why should you prefer making it friend and what are the rules, read this:

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    @howieh: You can always mark an accepted answer to your own question. Click on the outline of the tick mark to the left of the best answer. – Greg Hewgill Dec 07 '11 at 18:00
1

You need to use std::vector.push_back() or std::vector.insert() to insert elements inside a vector.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
0
// C++11
#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string>& operator << (vector<string>& op, string s) {
   op.push_back(move(s));
   return op;
}

int main(int argc, char** argv) {
    vector<string> v;

    v << "one";
    v << "two";
    v << "three" << "four";

    for (string& s : v) {
        cout << s << "\n";
    }
}
Bimo
  • 5,987
  • 2
  • 39
  • 61
0

Following piece of code appends to stream. similary you can add it to vector also.

class CustomAddFeature 
{
    std::ostringstream m_strm;

    public:

      template <class T>     
      CustomAddFeature &operator<<(const T &v)     
      {
          m_strm << v;
          return *this;
      }
};

as it is template so you can use it for other types also.

Azodious
  • 13,752
  • 1
  • 36
  • 71