std::vector

This is a discussion on std::vector within the C++ Programming forums, part of the General Programming Boards category; Can I overload operators +/-* for std::vector? Also how can I pass it into functions? Code: #include <iostream> #include <iomanip> ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    65

    std::vector

    Can I overload operators +/-* for std::vector? Also how can I pass it into functions?

    Code:
    #include <iostream>
    #include <iomanip>
    #include <stdio.h>      // standard input/output
    #include <vector>       // stl vector header
    
    using namespace std;    // saves us typing std:: before vector
    
    int i;
    int Particles = 5;
    
    vector<double> addition(vector<double> X, vector<double> Y){
    
     int Size = (int)X.size();
     vector<double> Z;
    
      for(i = 0; i < Size; i++){
    
        Z.push_back( X[i] + Y[i]);
    
          }
    
      return  Z;
    }
    
    int main()
    {
    
      // create an array of particle_field object pointers
    
      double y;
      vector<double> x_p, y_p, u_p, v_p, T_p;
    
      // dynamically add some elements (use new operator)
    
      for(i = 0; i < Particles; i++){
    
      x_p.push_back(0);
      y = 0.3/(Particles - 1)*i;
      y_p.push_back(y);
      u_p.push_back(0.6);
      v_p.push_back(0);
    
      }
    
    T_p = 3*v_p + x_p;
    
    T_p = addition(x_p, u_p);
    
    }

    Shuo

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Can I overload operators +/-* for std::vector?
    Yes, but there is probably no point, especially when it not clear what adding two containers really means. Just use std::transform from <algorithm> with the appropriate function object.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    I think it could do you a little good to brush up on your indentation skills:
    http://cpwiki.sf.net/User:Elysia/Indentation
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    65
    Thanks for the advice

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You can, but you're not allowed to, unless you involve one of your own classes in the overload.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    You can, but you're not allowed to, unless you involve one of your own classes in the overload.
    I had the impression that since it is illegal to add to the std namespace except to specialise class templates, one could overload an operator involving only class types from the standard library as long as the operator is not in the std namespace. Where does the C++ Standard prohibit operator overloads that only involve class types from the standard library?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Huh, seems I confused that one with template specializations.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you're using vector to implement some other concept or object, then you can make a class and use a vector as the implementation, then overload the operators for that class if it is appropriate. This has the added benefit of allowing you to change the container you use if you find a better way of implementing the interface.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using std::vector as a "memory stream"
    By 39ster in forum C++ Programming
    Replies: 9
    Last Post: 06-07-2009, 02:39 PM
  2. Problem with std::vector
    By ldb88 in forum C++ Programming
    Replies: 2
    Last Post: 02-08-2009, 12:18 AM
  3. passing std::vector and optimisation
    By l2u in forum C++ Programming
    Replies: 10
    Last Post: 07-03-2008, 11:01 AM
  4. std::vector issues...
    By IndioDoido in forum C++ Programming
    Replies: 14
    Last Post: 11-25-2007, 09:01 PM
  5. Problem converting std::vector into a reference
    By drrngrvy in forum C++ Programming
    Replies: 18
    Last Post: 10-12-2006, 09:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21