473,588 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with std::istream_it erator

Hi,

I am just a newbie in STL and trying to learn istream_iterato r. My
problem is I want a program which will take a comma separated string
from the command line, tokenize it and copies into a vector. Then I am
printing the vector. Here is the code I tried.

#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <cassert>

int main(int argc, char* argv[])
{
assert ( argv[1] );
std::vector <std::stringv ;
std::istringstr eam iss_st ( argv[2] );
std::istringstr eam iss_en ( "\n" );
std::istream_it erator < std::string st ( iss_st );
std::istream_it erator < std::string en ( iss_en );

std::copy ( st, en, std::back_inser ter ( v ) );
std::copy ( v.begin(), v.end(), std::ostream_it erator <std::string(
std::cout, "-" ) );
return 0;
}

But this code is aborting while running. I know I am missing something
very silly but not sure what.

Is there a better way of achieving the same thing though a different
way ?

Please help.

Thanks

Sep 18 '06 #1
8 3097
In article <11************ **********@b28g 2000cwb.googleg roups.com>,
pk******@gmail. com says...
Hi,

I am just a newbie in STL and trying to learn istream_iterato r. My
problem is I want a program which will take a comma separated string
from the command line, tokenize it and copies into a vector. Then I am
printing the vector.
One possible method: http://tinyurl.com/j63pz

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 18 '06 #2

Jerry Coffin wrote:
In article <11************ **********@b28g 2000cwb.googleg roups.com>,
pk******@gmail. com says...
Hi,

I am just a newbie in STL and trying to learn istream_iterato r. My
problem is I want a program which will take a comma separated string
from the command line, tokenize it and copies into a vector. Then I am
printing the vector.

One possible method: http://tinyurl.com/j63pz
I looked into the link, but that method seems to assume I know how many
fields are going to be there in the string. But in my case, there can
be any number to comma separated fields and I am not able to understand
why it can't be done using the istream_iterato r in the right way.

Sep 18 '06 #3
dragoncoder wrote:
Hi,

I am just a newbie in STL and trying to learn istream_iterato r. My
problem is I want a program which will take a comma separated string
from the command line, tokenize it and copies into a vector. Then I am
printing the vector. Here is the code I tried.

#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <cassert>

int main(int argc, char* argv[])
{
assert ( argv[1] );
I think you mean something like "assert (argc 1)". You can only access
members of argv between 0 and argc - 1. Argv contains the command line
arguments with argv[0] being the name of your program, argv[1] is the
first argument, etc. Argc is the number of elements in argv.

Also assert() is probably not a good idea here, because it may expand to
nothing depending on the compilation options you use.
std::vector <std::stringv ;
std::istringstr eam iss_st ( argv[2] );
You mean "argv[1]".
std::istringstr eam iss_en ( "\n" );
std::istream_it erator < std::string st ( iss_st );
std::istream_it erator < std::string en ( iss_en );

std::copy ( st, en, std::back_inser ter ( v ) );
The right way to construct an end iterator is by using its default
constructor -- you don't need iss_en at all, just don't pass anything to
"en".
std::copy ( v.begin(), v.end(), std::ostream_it erator <std::string(
std::cout, "-" ) );
You might want to print out a newline at the end:

std::cout << std::endl;
return 0;
}
Now this of course will split the input on white space, *not* on commas.
There are several ways to deal with commas, with or without
istream_iterato r. For example you can define a wrapper class with an
overloaded "operator >>" that skips over commas (somebody posted a
similar example here recently, in the "Locating partially matched
strings in a file" thread).

D.
Sep 18 '06 #4
In article <11************ **********@h48g 2000cwc.googleg roups.com>,
pk******@gmail. com says...

[ ... ]
I looked into the link, but that method seems to assume I know how many
fields are going to be there in the string.
I'm not sure what gave you that impression, but it's incorrect.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 18 '06 #5
Davlet Panech wrote:
dragoncoder wrote:
> assert ( argv[1] );

I think you mean something like "assert (argc 1)". You can only access
members of argv between 0 and argc - 1. Argv contains the command line
arguments with argv[0] being the name of your program, argv[1] is the
first argument, etc. Argc is the number of elements in argv.
I don't have a copy of ISO 9899 around, but isn't argv[argc] guaranteed
to be NULL?
Sep 18 '06 #6
red floyd wrote:
Davlet Panech wrote:
>dragoncoder wrote:
>> assert ( argv[1] );

I think you mean something like "assert (argc 1)". You can only
access members of argv between 0 and argc - 1. Argv contains the
command line arguments with argv[0] being the name of your program,
argv[1] is the first argument, etc. Argc is the number of elements in
argv.

I don't have a copy of ISO 9899 around, but isn't argv[argc] guaranteed
to be NULL?
Note: I mentioned 9899 because I'm assuming argv behavior is inherited
from C. My copy of 14882 is at work, alas.
Sep 18 '06 #7
red floyd wrote:
Davlet Panech wrote:
>dragoncoder wrote:
>> assert ( argv[1] );

I think you mean something like "assert (argc 1)". You can only
access members of argv between 0 and argc - 1. Argv contains the
command line arguments with argv[0] being the name of your program,
argv[1] is the first argument, etc. Argc is the number of elements in
argv.

I don't have a copy of ISO 9899 around, but isn't argv[argc] guaranteed
to be NULL?
Yes you are right, argv[argc] is required to be 0 in ISO 14882, section
3.6.1-2.

But its wrong either way since the OP was looking to extract the first
argument from the command line.

D.
Sep 18 '06 #8
In article <bP************ *****@newssvr25 .news.prodigy.n et>,
no*****@here.du de says...

[ ... ]
I don't have a copy of ISO 9899 around, but isn't argv[argc] guaranteed
to be NULL?
Yes -- section 5.1.2.2.1/2, bullet 2. The same is true in C++ (section
3.6.1/2).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 18 '06 #9

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

Similar topics

3
2146
by: NPC | last post by:
Hi, Is there any way to use an istream_iterator<> in a way which inserts each element at the end of a newline character rather than a space character? Could be it looks for any type of whitespace - not sure about that. In particular, it would be nice to use it in a way which is similar to an ostream_iterator: std::vector<std::string> myVec; /* add stuff to myVec */
4
2366
by: Jason Heyes | last post by:
Stupid templates won't compile. Here is my program: #include <fstream> #include <iostream> #include <algorithm> #include <iterator> #include <map> using namespace std; template <class T, class U>
2
1643
by: alberto | last post by:
I am learning STL with the book STL Tutorial and Reference guide (1 edition), the following example don't run : int main() { // Initialize array a with 10 integers: int a = {12, 3, 25, 7, 11, 213, 7, 123, 29, -31}; // Find the first element equal to 7 in the array: int* ptr = find(&a, &a, 7);
2
7047
by: Marcus Kwok | last post by:
I am writing a program to read in a file, do some processing, then write it out to a different file. I really like the idiom I use for output: // std::vector<std::string> vec; // std::ofstream out; std::copy(vec.begin(), vec.end(), std::ostream_iterator<std::string>(out, "\n"));
5
2820
by: Pradeep | last post by:
Hi All, I am facing some problem using istream_iterator for reading the contents of a file and copying it in a vector of strings.However the same thing works for a vector of integers. The code that doesn't work is std::vector<std::stringvecStr; std::ifstream fstrRead("Test.txt");
2
1803
by: Juha Nieminen | last post by:
I'm using gcc 3.3.5. This code: std::set<std::stringt(std::istream_iterator<std::string>(std::cin), std::istream_iterator<std::string>()); gives a strange error message: error: cannot use `::' in parameter declaration If I try it this way:
19
4762
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price, sell price, and a taxable flag (a Y/N char) and then write this all out to a file (preferably just a plain old text file) and then read it in later so that I can keep a running inventory. The problem that I am running into is when I write to the...
1
3657
by: faz | last post by:
HI all,, I used linux g++ 3.2.3 ...In my c++ code i am reading bit values from a file as character..using the following: char revStr,correctStr; int count=0 ifstream in("config.txt",ios::in | ios::binary); if(!in)
0
1438
by: Sam | last post by:
Andre Paim writes: Becase std::istream_iterator<chardoes not mean "return one character at a time from a std::istream". It means "return the next element from a std::istream, where each element is a single character, and all elements are separated by whitespace characters". It just so happens that 0x0c is the ascii formfeed character, which is a whitespace character, to it is interpreted as a delimitor between the 0x01
0
7929
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8228
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8357
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7987
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8223
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6634
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5398
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1459
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.