Thread: assign string to int array

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    7

    assign string to int array

    Hi all,

    I am new to C++.Any help would be appreciated.

    I have the following code:

    int myArray[20];
    string str= "1,2,3,8,10,12";

    I would like to assign str to myArray in that a way that the result will be as follow:
    myArray[0] = 1
    myArray[1] = 2
    myArray[2] = 3
    myArray[3] = 8
    myArray[4] = 10
    myArray[5] = 12

    May I know how to do that?I have try many times but unable to get the desired result. Thank you.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You can try strtok(), then assign each result to each element of the int array.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Unfortunately, strtok() does not play well with std::string.

    One idea is that given a string like "1 2 3 8 10 12", it is easy to do what you want to do: initialise a stringstream with that string, then use operator>> to extract the numbers in a loop. Thus, you can change the format of your input string to this format simply by replacing the commas with spaces.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    and instead of a c-style array of integers, I'd recommend a vector. it automatically performs bounds checking, and resizes itself automatically if you put too many items in it.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hey I picked up a C++ book at the library yesterday

    So I thought one thing to do here would be create a class derived from string so you could add a "extract_number(pos)" method, but...
    Code:
    class nlist : public string {
    	string str;
    	public:
    		nlist(string in) {
    			str.assign(in);
    		}
    };
    The resulting object does not seem to have inherited much, I guess because the std::string methods don't refer to my "string str"?

    How can you/should you do this?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    So I thought one thing to do here would be create a class derived from string so you could add a "extract_number(pos)" method
    That is a bad idea: std::string is not designed/intended to be a public base class. Ironically, a correct solution is what you are used to in C: create a non-member non-friend function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hey well I'm getting closer:
    Code:
    class nlist : public string {
    	public:
    		nlist(string in) {
    			assign(in);
    		}
    };
    Works.
    Quote Originally Posted by laserlight
    That is a bad idea: std::string is not designed/intended to be a public base class.
    So what is bad about it?
    Last edited by MK27; 02-22-2010 at 12:33 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    So what is bad about it?
    Conceptually, it is bad because you are just publicly inheriting to reuse an implementation, since std::string has no virtual functions to override. It makes the use of this function more difficult: to use it properly, you need an object of the derived class, yet one might want to use the function with std::string objects.

    Furthermore, in the event that an object of nlist is destroyed through a std::string pointer, undefined behaviour will result since std::string does not have a virtual destructor.

    You could inherit privately instead, but then users of your class will not have access to the various string functions unless you re-expose them.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    Conceptually, it is bad because you are just publicly inheriting to reuse an implementation, since std::string has no virtual functions to override. It makes the use of this function more difficult: to use it properly, you need an object of the derived class, yet one might want to use the function with std::string objects.
    Okay, well I agree with that: it is probably not a good idea from a design point of view, just wanted to make sure it was not flawed in a more profound sense.

    Furthermore, in the event that an object of nlist is destroyed through a std::string pointer, undefined behaviour will result since std::string does not have a virtual destructor.
    So avoid doing that, is what you are saying.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    Unfortunately, strtok() does not play well with std::string.
    Doh! I thought it was a char* array. That's what happens when I'm in a rush.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    7

    Smile Problem solved

    Hi all

    Thank for your reply and help.I have solved it by using stringstream as suggested by laserlight .

    Cheer!1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anyone help?
    By javalurnin in forum C Programming
    Replies: 11
    Last Post: 12-02-2009, 06:02 AM
  2. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM