Thread: A couple of questions

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    56

    A couple of questions

    I am learning the STL and have some questions.
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;
    
    int main()
    {
    	vector<string> vec;
    	vec.push_back("test");
    
    	string s(vec[0]);	//turn vec[0] into a string named s
    	std::string c( s.size(), char() );
    	cout << c << endl;
    	copy( s.begin(), s.end(), c.begin() );
    	cout << c << endl;
    
    	
    	return 0;
    }
    1. In the first cout, why doesn't c print out?
    2. If I eliminate std::string and define string c above, this doesn't work. I get an error: no match for call to ‘(std::string) (size_t, char)’. What is it that std::string is doing in this case?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Because at first c contains only characters '\0' which are non-printable.

    Well, for me it outputs spaces, which you could see when you printed something around it:

    Code:
    cout << "'" << c << "'" << endl;
    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).

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    56
    Because at first c contains only characters '\0' which are non-printable.
    So is this similar to how C puts '\0' at the end of an array only at the beginning of a string? And why do I need std::string in front of c( s.size(), char() );? If I declare c as a string and remove std::string, I get an error.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by swappo
    And why do I need std::string in front of c( s.size(), char() );? If I declare c as a string and remove std::string, I get an error.
    I am not sure what you mean. Please post the smallest and simplest program that demonstrates that error.
    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

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    56
    Here is the code with the changes as compared to the original in the first post.
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;
    
    int main()
    {
    	vector<string> vec;
    	vec.push_back("test");
    	string c;//added
    
    	string s(vec[0]);	
    	c( s.size(), char() );//deleted std::string
    	cout << c << endl;
    	copy( s.begin(), s.end(), c.begin() );
    	cout << c << endl;
    
      	
    	return 0;
    }
    Code:
    string_copy1.cpp: In function ‘int main()’:
    string_copy1.cpp:13: error: no match for call to ‘(std::string) (size_t, char)’

    Also, If I change some of the code to the following:
    Code:
    std::string c( s.size(), char() );
    	for(int i = 0; i < c.size(); i++)
    		cout << c[i];
    	cout << endl << c.size();
    I find that, cout << c[i];, produces all spaces yet c.size() prints out the number 4. Is c( s.size(), char() ) an integer? I hope I've explained this well enough. I am really confused on this.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can only call the constructor when you define the variable. Just like you had
    Code:
    std::string c( s.size(), char() );
    you can have
    Code:
    string c( s.size(), char() );

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The former calls the constructor and the latter calls operator () (of which there is no defined for std::string).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Couple of Questions
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 12-03-2005, 06:47 PM
  2. Couple of simple directdraw questions.
    By Deo in forum Game Programming
    Replies: 3
    Last Post: 05-25-2005, 07:55 AM
  3. Studying for Final, Couple Questions...
    By stewade in forum C Programming
    Replies: 4
    Last Post: 05-10-2004, 05:02 PM
  4. A couple of PowerPoint questions.
    By sean in forum Tech Board
    Replies: 2
    Last Post: 01-27-2003, 05:26 AM
  5. New to C++/ Couple of questions...
    By danielthomas3 in forum C++ Programming
    Replies: 13
    Last Post: 04-14-2002, 03:46 PM