copy unknown number of characters from cstring


Let's say I have a cstring: char str[] = " world";
and I have a needle: char n[] = "wo";
I find that the needle starts at the 5+1 position of the string.
How am I going to copy the characters up to this point, using the
header

--
Using (c++ string) it is possible in two ways:
1) for (int counter = 0; i < str.find(n); i++)
tmp += str[i];
2) str.substr();

Posted On: Sunday 4th of November 2012 11:15:49 PM Total Views:  161
View Complete with Replies

RELATED TOPICS OF C Language PROGRAMMING LANGUAGE




auto_ptr and copy ctr question

Soumen wrote: > If I've class with one of data members of auto_ptr type, how should I > write copy ctr or copy assignment operator if I don't intend to allow > transfer of ownership happen Declaring that member const could be one > option I guess. Clarify me if this understanding is wrong. Do I need > to disable these (copy ctr and assignment operator) in such cases > What happens when I don't write one and use the compiler generated > one Does transfer of ownership still happens or it's same as > declaring member as const Looks like disabling these two are best > design decision. If you don't provide the "move semantics" offered by the auto_ptr itself, you could do two things. One, as you mentioned, is to prohibit the copying altogether. That's a bit harsh to me, but if your design can endure that, it's the simplest of the two. The other is to provide copying of the owned object by copy-creating a new one in case of the constructor and copy-assigning the pointed-to item in case of the assignment: class Has_auto_ptr { std::auto_ptr myPtr; public: ... Has_auto_ptr(Has_auto_ptr const& other) : myPtr(new sometype(*other.myPtr)) {} Has_auto_ptr& operator =(Has_auto_ptr const& other) { *myPtr = *other.myPtr; return *this; } }; Disclaimer: the code is unchecked, provided for illustration purposes only. V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask , On May 29, 3:16 pm, Soumen wrote: > If I've class with one of data members of auto_ptr type, how > should I write copy ctr or copy assignment operator if I don't > intend to allow transfer of ownership happen Declaring that > member const could be one option I guess. Clarify me if this > understanding is wrong. Do I need to disable these (copy ctr > and assignment operator) in such cases What happens when I > don't write one and use the compiler generated one Does > transfer of ownership still happens or it's same as declaring > member as const Looks like disabling these two are best > design decision. It depends on the deed semantics. If transfer of ownership is not deed, you have two choices: don't support copy (by declaring the copy constructor private, and not providing an implementation), or write a copy constructor which does a deep copy. -- James Kanze (GABI Software) email:james.kanze@gmail.com Conseils en informatique oriente objet/ Beratung in objektorientierter Datenverarbeitung 9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34 , Soumen wrote: > [..] >
VIEWS ON THIS POST

122

Posted on:

Saturday 3rd November 2012
View Replies!

slicing - copying or assigning derived object to base object

Consider class Base { .... }; class Derived : public Base { ... }; Derived d_obj; As per my understanding, slicing happens in the following scenarios and they should be avoided. However my question is, will we ever require one of the following scenarios 1) Will there be any need to instantiate Base like Base b_obj(d_obj); // derived object sliced down to base object 2) Will there be any need to assign like b_obj = d_obj; // again derived object sliced down to base object 3) void fn(Base base_obj_arg) { ... } fn(d_obj); // slicing happens here 4) Base fn(const Derived& arg) { return arg; } Base temp = fn(d_obj); // again slicing happens here. What I am trying to say is, if the above four scenarios are avoided, slicing will not happen. However will there be any need for one of the above four scenarios to be used as mandatory Kindly explain.
VIEWS ON THIS POST

157

Posted on:

Saturday 3rd November 2012
View Replies!

copy derived structure, is it legal c++ code?

I am trying to copy some variables from a struct to a derived struct. If I have something like, .... struct STRUCTA { int numA; long numB; float numC; } struct STRUCTB public STRUCTA { int numD; } .... STRUCTA structA = {0,1,2}; STRUCTB structB; memcpy( &structB, &structA, sizeof( STRUCTA) ); .... // Is the above code legal to copy the structure from structA to structB
VIEWS ON THIS POST

122

Posted on:

Saturday 3rd November 2012
View Replies!

Should one use std::string*, std::map* to avoid copying ?

This is a very simplified version of something I am trying to understand. The State object holds the strings and maps , and I pass a reference to State to the process Function which manipulates it based on the tag passed in. I cannot pass the strings/maps in question as references coz .. a) thats redundant, state has everything, it increases number of parameters unnecessarily b) I have to do the case/switch outside the function to decide what to pass in.( and this is a simple example ) Is it a good idea to use things like string* and std::map..* as i have tried below
VIEWS ON THIS POST

225

Posted on:

Saturday 3rd November 2012
View Replies!

private copy ctor for exception handling class

Consider the following program: #include #include using namespace std; class Test { public: Test(); inline int value() const; private: Test(const Test &rhs); int x; }; Test::Test() { cout
VIEWS ON THIS POST

132

Posted on:

Sunday 4th November 2012
View Replies!

copy file

, its a function which copies file "f_from" to "f_to". Would you please tell me any condition when this function fail Assuming that I have proper file permissions... I want to apply best buffering and fastest work, thanks for any ideas. FILE *f_from; FILE *f_to; char buff[4096]; int r=0, s; while ( !feof (f_from) ) { s = fread( buff, 1, 4096, f_from) ; fwrite( buff, s, 1, f_to) ; }
VIEWS ON THIS POST

104

Posted on:

Sunday 4th November 2012
View Replies!

How to safely copy a struct

I am working with a struct (provided by an API I need to use) which has int arrays, char* and char buffers. I have been told there may be a problem because in previous code struct was copied using = and a deep copy is required. Do I have to copy each item in struct individually eg strcpy(sttarget.szString, stsource.szString); sttarget.intitem = stsource.intitem; etc Or is there some other effective way Angus
VIEWS ON THIS POST

96

Posted on:

Sunday 4th November 2012
View Replies!

Objects returned by value don't use copy constructors!

Irek Szczesniak wrote: > > > I thought that a copy constructor has to be involved, when a function > returns an object by value. It doesn't have to be so, as shown by > running the code at the end of my email (I used GCC 3.4.6 on Linux). Well, although your conclusion is correct this time: usually the result of a compiler should not be generalized to behavior mandated by the standard (as the phrase "it doesn't have to be so" seems to suggest). > (...) > I expected to see the copy constructor called once, and the > destructor twice. I would appreciate an explanation why this happens. > The compiler is allowed to optimize such copies (search C++ return value optimization). It effectively constructs the returned object before the function call, and the function directly operates on the outer object. The copy/assignment is optimized away. Try to run the code with debug settings used for compilation. Bye, Markus > >
VIEWS ON THIS POST

131

Posted on:

Sunday 4th November 2012
View Replies!

Conditional copying using stl functions

I've two vectors vectorA and vectorB. I've copy some elements from vectorA to B on satisfying some conditions. I'd like to avoid for loop and comparison routines. It would appreciate if there are any stl routines/classes can help on this matter. I tried using for_each but still passing elements still a problem. e.g stuct Test { char name[20]; int nCategory; } void Filter( const vector& vecA, vector& vecB, char* pFilter) { // compare "name" attribute with "pFilter" and copy on success }
VIEWS ON THIS POST

194

Posted on:

Sunday 4th November 2012
View Replies!

copy constructors with iterators

I've been trawling around for an answer to this question and thought I'd try here. I have a class Graph, which has a std::list as a class member. Node it itself a class that makes extensive use of pointers to represent various forms of data in the nodes of graphs, along with pointers to neighboring nodes. This necessitates a deep copy that iterators over the list. My copy constructor looks like: DecomposableGraph:ecomposableGraph(const DecomposableGraph& G) { // Some stuff // And now the iterator declaration: std::list::iterator jt_i, jt_l, jt_m; // use these iterators to do deep copy of G } The GNU compiler on my G4 Mac laptop does not object to this declaration, which I cobbled together by trial and error message. The code compiles, and the copy constructor works exactly as intended. The GNU compiler on a Linux machine that I want to port this code to, however, objects, claiming I can only have 1 member of the list template declaration rather than 3. This leads me to believe I've stumbled upon a peculiarity of the Mac implementation of the STL which makes my declaration interpretable, albeit nonstandard. The problem, as I understand it, is that I can't use a normal list::iterator to iterator over class members of the const G the way I could over a normal non-const function argument. But I don't know how to modify the declaration in general to make such iteration possible. Can anyone clue me in to the "standard" way to solve this problem, or correct my understanding
VIEWS ON THIS POST

146

Posted on:

Sunday 4th November 2012
View Replies!

copy ctor vs default ctor

If we do not provide any ctor for a class, the compiler provides the default ctor and copy ctor if needed. Consider a class Test. Suppose we provide some ctor in class Test but do not provide the default ctor. Suppose we try to create Test obj; Here the default ctor is needed and the compiler does not provide the default ctor but generates a compilation error. However if we provide some ctor but do not provide the copy ctor, the compiler does not generate error but provides the copy ctor if needed. Why doesn't the compiler give error for the absence of copy ctor similar to error for the absence of default ctor. Kindly explain
VIEWS ON THIS POST

117

Posted on:

Sunday 4th November 2012
View Replies!

copy values from

i have to vectors. std::vector a; std::vector b; same type, size and have synchronous value pairs. The values in "a" are decimal time values (linear increasing). Now i want to copy in a 3rd vector "c" all elements from a start value to a end value. My first idea was something like: std::vector c; // target for (std::vector::iterator it=a.begin(); it != a.end(); it++) { if (*it > startvalue && *it < endvalue) { c.push_back(); } } do i need a 2nd iterator for vector b ! but how or ist there a easier was to get the values to vector c
VIEWS ON THIS POST

105

Posted on:

Sunday 4th November 2012
View Replies!

Examples of the difference between copy-initialization and direct-initialization

Can anyone give me examples of the difference between copy- initialization and direct-initialization. That is, expressions which are allowed as one and not the other I can come up with a trivial one: ----------------- struct foo { explicit foo(int) {} }; void f() { foo f(3); // Error foo g = 3; // owed } ----------------- I can also come up with a complex one: ----------------- #include struct foo { }; struct base { base() {} explicit base(const foo&) { std::cout
VIEWS ON THIS POST

140

Posted on:

Sunday 4th November 2012
View Replies!

remove_copy

Dear all, it may be a friday afternoon issue, but I was looking for a remove variant which removed elements from a vector, but not throwing them away in the first place, but keeping a copy so that I still could do things with the removed elements before actually destroy them. So in the STL you have the 'remove(_if)' function which removes elements. However the 'remove_copy' does not remove elements form the original input! It has more the semantic of a 'copy_if_not' function (also implicitly pointed out by Austern, by implementing 'copy_if' in terms of 'remove_copy_if', see 12.6.4 of Generic Programming && STL). Since the STL already exist for more than a decade, it must be my own mind which interprets the function name wrong. Or it is indeed a friday afternoon issue. Of course human language is multiple interpretable and English is not my native language, so the original c+ + committee members may be 'right' after all. Above functionality is solvable btw by stable_partition as someone pointed out in CUJ. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
VIEWS ON THIS POST

124

Posted on:

Sunday 4th November 2012
View Replies!

copying contents from a char array to std::string obj

Hi. Assuming I have a code snippet like below: #include using namespace std; char Mac[6] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5 }; std::string csMac; 1. Whats the best way to do a deep copy from the c-style char array to the c++ string object. I know the other way around to assign to the string object (to use a c_str() on the string).
VIEWS ON THIS POST

119

Posted on:

Sunday 4th November 2012
View Replies!

copy data on screen

i have small gaem which pick a word randomly from a list of 100 four letterd words and asks user to guess it, when user inputs it, program compares the inputted word with the randomly picked word and return results, user gets 10 chances to guess the correct word. famous "COWS n BULLS" now the problem is that program has 4 options ,start , resume, help screen and exit. now if a user wants to see help screen in between the game , the current game will be lost and a new game will start on selecting start. i want that if a user selects help screen in between a running game then the screen should be copied and should be printed on rselection of resume. any ideas \t\t\t
VIEWS ON THIS POST

99

Posted on:

Sunday 4th November 2012
View Replies!

fast array copy c++

Dear developers, I have a question. Is there a faster array copy than a for loop. What would you suggest for this code sample void get(char* src, char* dest, int i) { for (int j = 0; j < recordLen; j++, i++) { dest[j] = src[i]; } } Thank you for your answers.
VIEWS ON THIS POST

103

Posted on:

Sunday 4th November 2012
View Replies!

copying elements from a to

It works fine. any advice on making it better or if I can improve my C++ coding skills: /* C++ Primer - 4/e * * Chapter 9 - Sequential Containers * exercise 9.18 - STATEMENT * Write a program to copy elements from a list of "ints" * to 2 "deques". The list elements that are even should go into one deque * and even elements should go into 2nd deque. * */ #include #include #include #include #include int main() { std::cout
VIEWS ON THIS POST

111

Posted on:

Sunday 4th November 2012
View Replies!

copy() for string

In Delphi, there's the copy( source, dest, indx) function. Does it exist in C++ This copies from the beginning of the string to the index value.
VIEWS ON THIS POST

200

Posted on:

Sunday 4th November 2012
View Replies!

copy-initialization and class object as static member

Consider the following program: #include using namespace std; class Base { public: Base(int x = 0); private: Base(const Base & arg); int val; static Base obj; }; Base::Base(int x) : val(x) { cout
VIEWS ON THIS POST

215

Posted on:

Sunday 4th November 2012
View Replies!