loading vectors and data before main

This is a discussion on loading vectors and data before main within the C++ Programming forums, part of the General Programming Boards category; I have a series of datasets that I load like this: ifstream code that looks like this in the beginning ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    24

    loading vectors and data before main

    I have a series of datasets that I load like this:

    ifstream code that looks like this in the beginning of main
    Code:
    int main
    {
     vector<vector<bool >> setbool;
    ifstream setboolCSV ("setboolcsv.csv");
    linenum = 0;
    itemnum = 0;
    while (getline (setboolCSV, line))
        {
            ++linenum; //increment the line count
            setbool.resize(linenum);
    
            istringstream linestream(line);
    	itemnum = 0;
            while (getline (linestream, item, ','))
    		{
    			++itemnum; //increment the item count
                            setbool[linenum-1].resize(itemnum);
    			const char *pitem = item.c_str();
    			doubleitem = atof(pitem);
    			setbool[linenum-1][itemnum-1] = doubleitem;
    			 
    		}
    	
    	}
    
    setboolCSV.close();
    ...
    return 0;
    }
    end of code
    however classes that I create will be reliant on values in setbool based on indexes that i create. Is there a way to create bool before main, but pass it into main as well as any class header files that i create? what i am trying to avoid is if I have 10 classes that uses setbool[X][Y] to make a decision, i don't want to create 10 instances of setbool.

    this code works as is - any thoughts also on how to structure to clean up main ? i realize that it is not best to convert from double into bool, but it is ok b/c my file is 1s and 0s. 0s will turn into false, 1s into true. if there is better way to write please let me know.
    Last edited by lawrenced; 07-29-2009 at 04:51 PM.

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Is there a way to create bool before main, but pass it into main as well as any class header files that i create?
    Make it global ?
    You could create a header files containing your global variable and declare it as extern where you want to use it.
    Spidey out!

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    Quote Originally Posted by Spidey View Post
    Make it global ?
    You could create a header files containing your global variable and declare it as extern where you want to use it.
    how to declare it as external? do you have code example of declare?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by lawrenced View Post
    I have a series of datasets that I load like this:

    ifstream code that looks like this in the beginning of main
    Code:
    int main
    {
     vector<vector<bool >> setbool;
    ifstream setboolCSV ("setboolcsv.csv");
    linenum = 0;
    itemnum = 0;
    while (getline (setboolCSV, line))
        {
            ++linenum; //increment the line count
            setbool.resize(linenum);
    
            istringstream linestream(line);
    	itemnum = 0;
            while (getline (linestream, item, ','))
    		{
    			++itemnum; //increment the item count
                            setbool[linenum-1].resize(itemnum);
    			const char *pitem = item.c_str();
    			doubleitem = atof(pitem);
    			setbool[linenum-1][itemnum-1] = doubleitem;
    			 
    		}
    	
    	}
    
    setboolCSV.close();
    ...
    return 0;
    }
    end of code
    however classes that I create will be reliant on values in setbool based on indexes that i create. Is there a way to create bool before main, but pass it into main as well as any class header files that i create? what i am trying to avoid is if I have 10 classes that uses setbool[X][Y] to make a decision, i don't want to create 10 instances of setbool.

    this code works as is - any thoughts also on how to structure to clean up main ? i realize that it is not best to convert from double into bool, but it is ok b/c my file is 1s and 0s. 0s will turn into false, 1s into true. if there is better way to write please let me know.
    If the class requires values from the data file, then you're going to have to wait until you read the data file (which will be after main starts). You can create instances of the class at any time, including after the file is read.

    Why are you reading 1's and 0's via double? Why not just read it in as a bool?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    You should pass it by reference to the classes that need it. You can either pass it into the class functions that need that data, or pass it in their constructor and let them store a reference to it.

    Also, I would look into the push_back function for vector. I think you'll like it better than using resize so much.

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    Quote Originally Posted by tabstop View Post
    Why are you reading 1's and 0's via double? Why not just read it in as a bool?
    because I don't know how to do this? is there a way?

  7. #7
    Registered User
    Join Date
    Jul 2009
    Location
    MD
    Posts
    10
    Here's a website with many of the most common C++ data types.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by lawrenced View Post
    because I don't know how to do this? is there a way?
    Code:
    #include <iostream>
    #include <sstream>
    #include <iomanip>
    
    int main() {
    
        std::stringstream foo;
        foo << "1 0 0 0 0 1 1 1 1 0 0";
        bool bar;
        while (foo >> bar) /*ooh reading into a bool!*/{
            std::cout << std::boolalpha << bar << std::endl;
        }
        return 0;
    }

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,765
    Quote Originally Posted by Spidey View Post
    Make it global ?
    You could create a header files containing your global variable and declare it as extern where you want to use it.
    Not a good idea. The initialization of objects is implemention-defined, so it's impossible to guarantee the file has been opened before using it.
    I would delay creating the classes until the file has been opened, and pass it as arguments to the constructors of the classes.
    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. Data Structure to store unlimited dynamic vectors
    By m3rk in forum C++ Programming
    Replies: 8
    Last Post: 04-22-2009, 06:12 AM
  2. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  3. How to input a data file to parallel vectors
    By Wink- in forum C++ Programming
    Replies: 13
    Last Post: 10-30-2007, 05:24 PM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Using Vectors (cont) - Sort Problem
    By clegs in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2007, 06:31 AM

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