Vector class -STL lib

This is a discussion on Vector class -STL lib within the C++ Programming forums, part of the General Programming Boards category; I am new with vectors and STL library. please tell where I'm going wrong? Code: int t,i; char ch; char ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    15

    Vector class -STL lib

    I am new with vectors and STL library. please tell where I'm going wrong?

    Code:
    	int t,i;
    	char ch;
    	char arr[400];
    	vector<char> exp;
    	stack<char> s;
    
    	cin>>t;
    	for(i=0;i<t;i++)
    	{
    		cin>>arr;
    		exp.push_back(arr);                      <<-- what's wrong here?
    	}

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are trying to push back an array - you could do that if you have a vector that holds arrays (e.g vector<char [400]> I think the syntax of that would be). If you actually want your vector to hold each character as an individual element, then you need to loop through the array and push_back each element.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Any reason why you don't use a string for input? Then you could easily use the insert method to append characters from the string to the vector.

    Code:
    string s;
    vector<char> v;
    v.insert(v.end(), s.begin(), s.end());
    (insert would work with char array too, but you'd first need to find out where the null terminator is.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Personally, I'd use something like this, assuming I've correctly guessed what you wanted to do.
    Code:
    vector<string> data;
    
    string line;
    while(getline(cin, line)) {
        data.push_back(line);
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,050
    Quote Originally Posted by matsp View Post
    You are trying to push back an array - you could do that if you have a vector that holds arrays (e.g vector<char [400]> I think the syntax of that would be).
    You can't have a vector of arrays, because arrays do not follow the same constructor syntax as classes and primitives. It may work if you specify an special allocator.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using VC6 link VC7 static lib error
    By George2 in forum C++ Programming
    Replies: 5
    Last Post: 06-29-2006, 10:58 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21