473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Import Only std::string

I'd like to avoid typing std::string and std::vector without resorting
to the "using namespace std" directive, which will import the entire
standard namespace. Is there a way to import only selected items from
the std namespace, so to speak?

Thanks for any ideas,
cpp
Jul 22 '05 #1
7 2660
cppaddict wrote:
I'd like to avoid typing std::string and std::vector without resorting
to the "using namespace std" directive, which will import the entire
standard namespace. Is there a way to import only selected items from
the std namespace, so to speak?


Make your pick:

using std::string; // then just type 'string'

or

typedef std::string str; // then just type 'str'

Victor
Jul 22 '05 #2
cppaddict <he***@hello.com> wrote in
news:4p********************************@4ax.com:
I'd like to avoid typing std::string and std::vector without resorting
to the "using namespace std" directive, which will import the entire
standard namespace. Is there a way to import only selected items from
the std namespace, so to speak?

Thanks for any ideas,
cpp


Uh...

using std::string;
using std::vector;

?
Jul 22 '05 #3
>Make your pick:

using std::string; // then just type 'string'

or

typedef std::string str; // then just type 'str'


Thanks Victor.

I like the first option. Would you recommend placing it only in my
header file, or repeating it in the implementation file too, for
clarity?

cpp
Jul 22 '05 #4
cppaddict wrote:
Make your pick:

using std::string; // then just type 'string'

or

typedef std::string str; // then just type 'str'

Thanks Victor.

I like the first option. Would you recommend placing it only in my
header file, or repeating it in the implementation file too, for
clarity?


I recommend against any 'using' directives or declarations in the header.
If you think you need them in the global scope, limit yourself to the
implementation files only. Of course, it would be best if you could
place such declarations in the same scope level where they are going to
be used. Example: if you're working with strings in a single function
in some module, no sense to put the 'using' declaration at the beginning
of the module that contains that function. Put it in the function itself.

Victor
Jul 22 '05 #5
>I recommend against any 'using' directives or declarations in the header.
If you think you need them in the global scope, limit yourself to the
implementation files only.


Would you object to putting "using std::string;" inside the class:

class MyClass {
using std::string;
private:
string name_;
.....
}

thanks again,
cpp
Jul 22 '05 #6
cppaddict wrote:
I recommend against any 'using' directives or declarations in the header.
If you think you need them in the global scope, limit yourself to the
implementation files only.

Would you object to putting "using std::string;" inside the class:

class MyClass {
using std::string;
private:
string name_;
....
}

thanks again,
cpp


Nope, but some compilers might object. ;-)

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #7

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:XI***************@newsread1.dllstx09.us.to.ve rio.net...
I recommend against any 'using' directives or declarations in the header.
If you think you need them in the global scope, limit yourself to the
implementation files only. Of course, it would be best if you could
place such declarations in the same scope level where they are going to
be used. Example: if you're working with strings in a single function
in some module, no sense to put the 'using' declaration at the beginning
of the module that contains that function. Put it in the function itself.


Whoa! You can do that?? Awesome!
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
8
by: Patrick Kowalzick | last post by:
Dear NG, I would like to change the allocator of e.g. all std::strings, without changing my code. Is there a portable solution to achieve this? The only nice solution I can think of, would be...
6
by: Nemok | last post by:
Hi, I am new to STD so I have some questions about std::string because I want use it in one of my projects instead of CString. 1. Is memory set dinamicaly (like CString), can I define for...
2
by: FBergemann | last post by:
if i compile following sample: #include <iostream> #include <string> int main(int argc, char **argv) { std::string test = "hallo9811111z"; std::string::size_type ret;
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.