Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.glorb.com!feeder.erje.net!news2.arglkargh.de!news.albasani.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Ian Collins Newsgroups: comp.lang.c++ Subject: Re: Best way to write the vector content to a file. Date: Tue, 20 Sep 2011 15:12:19 +1200 Lines: 24 Message-ID: <9dqekjF76kU6@mid.individual.net> References: <31086262.1123.1316487944574.JavaMail.geo-discussion-forums@yqaa12> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net L2nMi+wjeTDKxPFoqQnNqgboLyQyFKof/y2BSn8oJQGsZmtRij Cancel-Lock: sha1:LbY+9AfXysjDsUC+FoIsvLVow+A= User-Agent: Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4 In-Reply-To: <31086262.1123.1316487944574.JavaMail.geo-discussion-forums@yqaa12> Xref: x330-a1.tempe.blueboxinc.net comp.lang.c++:10312 On 09/20/11 03:05 PM, sravanreddy001 wrote: > Hi, > > Can anyone give the best way (faster) to write the vector contents to a file? > > Is there a better way to do it than below method? > > for(unsigned i=0;i sprintf(temp,"%d %s\n",i,terms[i].c_str()); > ids_n_terms = ids_n_terms + temp; > } Where do you do the write? > Is the string addition a very expensive operation? (will a new memory allocation takes place) > > there are around 5,00,000 (strings) in the vector, and these have to stored in > format. You will probably be I/O limited, so optimising your code won't make any significant difference. -- Ian Collins