473,385 Members | 1,582 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,385 software developers and data experts.

A vector of strings

Hello. I am writing code to put the traditional C-style command-line
arguments into a std::vector of std::strings. I was wondering: which
is the best way to do it? Are they identical?

#include <string>
#include <vector>

int mymain(const std::vector<std::string&);

int main(int argc, char *argv[])
{
std::vector<std::stringtheArgv;
for (int i = 0; i < argc; i++)
{
// a) or b) ?
}
return mymain(theArgv);
}

a) theArgv.push_back(argv[i]);

b) theArgv.push_back(std::string(argv[i]));

Any help is appreciated. :)
Jun 27 '08 #1
3 1452
am********@gmail.com wrote:
Hello. I am writing code to put the traditional C-style command-line
arguments into a std::vector of std::strings. I was wondering: which
is the best way to do it? Are they identical?

#include <string>
#include <vector>

int mymain(const std::vector<std::string&);

int main(int argc, char *argv[])
{
std::vector<std::stringtheArgv;
for (int i = 0; i < argc; i++)
{
// a) or b) ?
}
return mymain(theArgv);
}

a) theArgv.push_back(argv[i]);

b) theArgv.push_back(std::string(argv[i]));

Any help is appreciated. :)
What about

c)

int main ( int argn, char ** args ) {
std::vector< std::string argument ( args, args+argn );

Best

Kai-Uwe Bux
Jun 27 '08 #2
std::vector< std::string argument ( args, args+argn );

Could you please explain that line in detail? I don't seem to
understand what it does.
Jun 27 '08 #3
am********@gmail.com wrote:
> std::vector< std::string argument ( args, args+argn );

Could you please explain that line in detail? I don't seem to
understand what it does.
std::vector<Thas a constructor

template < Iterator >
vector ( Iterator from, Iterator to );

This constructor will construct a vector of Ts from any range whose elements
are convertible to T. Since char* converts to std::string, you can
construct a vector of strings from any range of char* Now, args is a
pointer to an array of char*, therefore

[args, args+argn)

is a range of char*.
Best

Kai-Uwe Bux
Jun 27 '08 #4

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

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
1
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without any sacrifice in performance. I'm writing an...
4
by: Matt Garman | last post by:
Is there any difference, performance-wise, accessing elements of a vector using iterators or the subscript operator? In other words, say I have a vector of strings: vector<string> strvec; ...
6
by: Joe | last post by:
I have a: vector<string> which contains a few dozen elements. I want to find the index of the element containing a certain string. for example: vector<string> strings;...
5
by: Abhishek Pandey | last post by:
Hi, I have a vector of strings. I want to hold it only unique strings. That is, before adding any new string, I want to check if it is already present, and if yes, then I will not add the new...
9
by: uotani.arisa | last post by:
Hi, Can someone tell me how to declare a pointer to a vector of pointers? I'm just not sure how to do this... I've tried essentially the following: vector<string *> * v; ....
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
5
by: Fred | last post by:
Hi: I've got the following request: (suppose the required head file is included) vector<stringvec; // .......... // push some items into vec string str; // here I want to copy some range...
10
by: Shafik | last post by:
Hello, I am new to C++. I know the reason is probably template instantiation problems ... but what's the *real* reason I cannot declare a: vector<stringv = vector<string>(4); Thanks!...
9
by: barcaroller | last post by:
1. If I pass pointers (char*) as iterators to an STL algorithm and the return value is an iterator, can I convert that iterator to a pointer? If yes, how? 2. What is the internal...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.