Solved

Two questions

Posted on 2004-04-23
4
171 Views
Last Modified: 2010-04-01

Consider

#include <iostream>
#define PRINT(X)  (std::cout<<#X": "<<(X)<<"\n")
#include <vector>
#include <string>

int main(void)
   {
   using std::vector;
   using std::string;
   vector<vector<string> > vec1;
   vector<string>vec2;

   vec2.push_back("hello");
   vec2.push_back("world");
   vec1.push_back(vec2);
   vec2.clear();

   vec2.push_back("another");
   vec2.push_back("vector");
   vec1.push_back(vec2);
   
   PRINT(vec1[0][1]);
   PRINT(vec1[1][1]);
   }

Output:
vec1[0][1]: world
vec1[1][1]: vector

////
After a closer look I envision that the  layout in memory amounts to

vec[0][0]              vec[0][1]                vec[1][0]                   vec[1][1]
 Hello                      world                     another                      vector

hence the result but the notion of '2 dimensional' vector when viewed from the code makes no sense to me.  Simply becasue prior to seeing the output I'd envisioned
                                    OR
vec2[0]   = hello           ==>   vec2[0][0]   hello
vec2[1]   = world          ==>   vec2[0][1]   world
vec2[2]   = another       ==>   vec2[0][2]   another
vec2[3]   = vector         ==>   vec2[0][3]   vector

where am i going wrong?  
In addition could I get sample code on a  3 dimensional vector?

--------------------------------------------------- 2 ---------------------------------------------------
The following snippet will print a hex address (memory offset?) that represents the memory location the pointer is pointing to:
    int i = 50;
    int* ip = &i;
    std::cout << ip << std::endl;

Changing the types to char and char*,
    char i = 50;
    char* ip = &i;
    std::cout << ip << std::endl;

prints garbage but cast ip to void* and

char i = 50;
char* ip = &i;     // or void* ip = &i;
std::cout << static_cast<void*>(ip) << std::endl;

the output is as expected.

I'm guess there's an implicit conversion from a int* to a void* and in that case cout behaves itself. On  the contrary there's no implicit conversion from char* to void* and as such cout has no idea (used sparingly) what to do  hence it outputs garbage.  So far so good?  If yes, then what does char* convert to when the output is 'garbage'.

Thanks for your time


0
Comment
Question by:forums_mp
  • 2
  • 2
4 Comments
 
LVL 15

Assisted Solution

by:efn
efn earned 50 total points
ID: 10905782
1.

vec2.clear();

clears the vector.  So "another" goes into vec2[0], not vec2[2].

vec2[0] is a string, so vec2[0][0] is a character.  Did you mean to refer to vec1 in your doubly-subscripted list?

There is no three-dimensional vector, but just as you can have a vector of vectors to approximate a two-dimensional vector, you can have a vector of vectors of vectors to approximate a three-dimensional vector:

vector<vector<vector<string> > > vec3;

2.

char* is a special case in stream output.  When you feed a character pointer to an output stream, it gets characters from where the pointer points and puts them out, until it gets a null character.  This is convenient for putting out C-style null-terminated character strings.  In your case, the pointer did not point to a null-terminated character string, so you should have seen the character for 50 first (which is '2' in ASCII) and then whatever was in memory after that until it hit a null character.  For pointers that are not pointers to characters, it just puts out the value of the pointer, as you saw.

--efn
0
 

Author Comment

by:forums_mp
ID: 10906409

Interesting how I pass the stream a pointer and the contents that the pointer points to are displayed.  I'd have thought I'd have to deference the pointer(ip) to get the contents at that address?

In other words
std::cout << *ip << std::endl;  // gives 2 - ascii for 50
std::cout << ip << std::endl;    // also gives 2(+ garbage) but without deferencing

-------------
you mentioned "after that until it hit a null character"
Since I didnt specify one(ie null character).  Where does 'it' find/insert a null?


0
 

Author Comment

by:forums_mp
ID: 10906417

I had one other question that slipped me.   You state "vec2[0][0] is a character."  Give vec2[0] is another, Which character is vec2[0][0]?

'Did you mean to refer to vec1 in your doubly-subscripted list?'  

Not sure I'm following you here

0
 
LVL 15

Accepted Solution

by:
efn earned 50 total points
ID: 10906522
> I'd have thought I'd have to deference the pointer(ip) to get the contents at that address?

Yes, that's why I called it a special case.  The designers figured that there were likely to be so many C-style null-terminated character strings lying around that if somebody passed a character pointer to an output stream, he probably wanted to put out the string, not the pointer, so they sacrificed consistency for convenience.  So in exchange for the confusion of a few inquiring minds like yours, presumably, thousands of programmers have had the convenience of being able to send null-terminated strings to output streams without having to convert them to some other streamable form first.

> Since I didnt specify one(ie null character).  Where does 'it' find/insert a null?

It doesn't insert a null character.  It just keeps reading through memory until it happens to find one.  It's looking at memory that has been allocated for some other purpose or has not been allocated and is waiting to be reused.  As you said, it's garbage.

> Give vec2[0] is another, Which character is vec2[0][0]?

It's the 'a' at the beginning of the string.  vec2[0][1] would be 'n'.

>> 'Did you mean to refer to vec1 in your doubly-subscripted list?'  

> Not sure I'm following you here

Your question showed:

 vec2[0][0]   hello

vec2 is declared

vector<string>vec2;

So vec2[0][0] is first 'h', then nonexistent, then 'a'.  Since vec1 is the vector of vectors and vec1[0][0] would contain "hello" as you showed above, I thought you may have meant to refer to vec1 instead of vec2 in the example.

--efn
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now