1

I am working on a very big and old C++ project. The word "vector" has been used all over the place. I am now trying to add new functionality to the project utilizing STL vector. This is not working. In addition, I am only allowed to modify specific sections of the code, so I can not change their use of "vector".

Is there a way to rename STL vector and use it?

user1247549
  • 193
  • 1
  • 9

5 Answers5

15

Use C++ namespaces.

#include <vector>

// note the absence of `using namespace std;` line

int main() {
    // note the `std::` qualification
    std::vector<int> x;

    // ...
}
ulidtko
  • 14,740
  • 10
  • 56
  • 88
5

As long as you don't put using namespace std; anywhere (which is not a great thing to do anyway), there will be no conflict between ::std::vector and any other vector.

I'm assuming that this infinite wisdom didn't extend to declaring names inside std; in that case, my best advice is to run away. I'm also assuming that you're talking about the modern C++ library, not the STL which (I think) didn't have its own namespace.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
3

Remove the using namespace std; from the beginning of the file, and add std:: in the code wherever is needed:

std::cout << "debug" << std::endl;
std::vector<std::string> simple_vector;
karlphillip
  • 92,053
  • 36
  • 243
  • 426
1

Just refer to it using the full name std::vector.

In C++11, you could also use a using statement.

dan04
  • 87,747
  • 23
  • 163
  • 198
-1

Don't do this.

#define vector stdVector
#include <vector>
#undef vector

End of don't do this

You can either use namespaces to qualify your version of vector or the one from std, after you remove the using directives.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625