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

Home Posts Topics Members FAQ

What the fastest way to copy strings from file to a vector?

What the fastest way to copy strings from file to a vector in STL?
Jul 23 '05 #1
7 2207
"SIgnOff" <Si*****@gala.n et> wrote...
What the fastest way to copy strings from file to a vector in STL?


In what sense "fastest"? And a vector of what? Assuming certain things,
one could write

std::vector<std ::string> vs;
std::string somestring;
std::ifstream somefile("somef ilename");
while (std::getline(s omefile, somestring))
vs.push_back(so mestring);

V
Jul 23 '05 #2
Check out boost serialization

http://www.rrsd.com/boost/libs/seria...doc/index.html

Raj

Jul 23 '05 #3
Victor Bazarov wrote:
"SIgnOff" <Si*****@gala.n et> wrote...
What the fastest way to copy strings from file to a vector in STL?

In what sense "fastest"? And a vector of what? Assuming certain things,
one could write

std::vector<std ::string> vs;
std::string somestring;
std::ifstream somefile("somef ilename");
while (std::getline(s omefile, somestring))
vs.push_back(so mestring);


Oh, come on, Victor, you're not going to use an istream_iterato r? :-)

std::vector<std ::string> vs;
std::ifstream somefile("somef ilename");
std::copy(std:: istream_iterato r<std::string>( somefile),
std::istream_it erator<std::str ing>(),
std::back_inser ter(vs));
Jul 23 '05 #4
red floyd wrote:

[ ... ]
std::vector<std ::string> vs;
std::string somestring;
std::ifstream somefile("somef ilename");
while (std::getline(s omefile, somestring))
vs.push_back(so mestring);


Oh, come on, Victor, you're not going to use an istream_iterato r? :-)

std::vector<std ::string> vs;
std::ifstream somefile("somef ilename");
std::copy(std:: istream_iterato r<std::string>( somefile),
std::istream_it erator<std::str ing>(),
std::back_inser ter(vs));


Note that these do _completely_ different things. Victor's code reads
each _line_ in the input into a string. Your code reads each _word_
into its own string.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #5
Jerry Coffin wrote:
red floyd wrote:

[ ... ]

std::vector<std ::string> vs;
std::string somestring;
std::ifstream somefile("somef ilename");
while (std::getline(s omefile, somestring))
vs.push_back(so mestring);


Oh, come on, Victor, you're not going to use an istream_iterato r? :-)

std::vector<s td::string> vs;
std::ifstre am somefile("somef ilename");
std::copy(std ::istream_itera tor<std::string >(somefile),
std::istream_it erator<std::str ing>(),
std::back_inser ter(vs));

Note that these do _completely_ different things. Victor's code reads
each _line_ in the input into a string. Your code reads each _word_
into its own string.


Good point. You're absolutely right. However, the original post
said "read strings into a vector". Either approach could be correct,
but I suspect OP meant newline delimited. So I concede the point.

I guess if you wanted to use the std::copy method, you'd have to define
a proxy class with operator >> that read an entire line, and also had an
"operator std::string() const" method. At which point we've long since
passed the point of diminishing returns.
Jul 23 '05 #6
red floyd wrote:

[ ... ]
Note that these do _completely_ different things. Victor's code
reads each _line_ in the input into a string. Your code reads
each _word_ into its own string.

Good point. You're absolutely right. However, the original post
said "read strings into a vector". Either approach could be correct,
but I suspect OP meant newline delimited. So I concede the point.


No point to concede, IMO. I wasn't trying to say either was right or
wrong, just that they're not the same. I don't think the OP provided
enough information to indicate which is desired.
I guess if you wanted to use the std::copy method, you'd have to
define a proxy class with operator >> that read an entire line,
and also had an "operator std::string() const" method. At which
point we've long since passed the point of diminishing returns.


There's another possibility -- imbue the stream with a locale that
defines the newline as its only space character. I'd tend to agree that
neither of these is practical unless you use them considerably more
than once.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #7
These codes widely well-known both.
Is any other fast ways?

-----------------
std::vector<std ::string> vs;
std::ifstream somefile("somef ilename");
std::copy(std:: istream_iterato r<std::string>( somefile),
std::istream_it erator<std::str ing>(),
std::back_inser ter(vs));
---------------
std::vector<std ::string> vs;
std::string somestring;
std::ifstream somefile("somef ilename");
while (std::getline(s omefile, somestring))
vs.push_back(so mestring);
Jul 23 '05 #8

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

Similar topics

4
2790
by: Venkat | last post by:
Hi All, I need to copy strings from a single dimensional array to a double dimensional array. Here is my program. #include <stdio.h> #include <stdlib.h>
6
2583
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a "SEGV" when run (presumably - attempt to delete deleted memory. Please take a look and see if you can notice any mistakes I'm making. Basically, I want to store classes of my objects in a vector. I also have three further questions:
6
8489
by: George | last post by:
Hi All, I'm trying to learn c++/stl. I'd like a fancy way to read lines of an ascii file into vector of stringbufs. I made a first attempt, but the compiler complains about private constructors in streambuf. I'd like to use algorithms instead of a loop, but I don't know if that is possible. Any ideas? Thanks in advance.
24
2709
by: kalamantina | last post by:
#include "stdafx.h" #include <stdio.h> #define output( x ) printf( #x "\r\n" );fflush( stdout ) class CMyBase { public: CMyBase() { output( CMyBase() ); f(*this);
9
6829
by: sshock | last post by:
Hi all, I want to read from a file into a vector<unsigned char>. Right now my code looks like this: FILE* f = fopen( "datafile", "rb" ); enum { SIZE = 100 }; vector<unsigned char> buf(SIZE); fread(&buf, 1, SIZE, f);
5
2839
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");
5
2394
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 of elements from vec to str, for example, from vec
5
3912
by: devdude | last post by:
Hi, I have the need to take a snapshot of a hash_map during execution (actually transform it to a vector). This map is a shared resource and therefore must be locked prior to any read/write operations thus I need to minimize the amount of time the map resource is locked. The map is defined as type <string, boost::shared_ptr<myobject>>. My algorithm is as such:
2
13075
by: Lambda | last post by:
The code is simple: // Token.h #ifndef TOKEN_H #define TOKEN_H #include <vector> #include <string> class Token
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
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
10384
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
10395
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
10130
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
9204
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...
1
7667
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6887
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();...
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.