Thread: Creating a class variable with a string

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    32

    Creating a class variable with a string

    Let's say I have defined a class in a header file; just the class, no templates involved. I have a program where I'm reading in data in string format. Each string consists of a word, a delimiter, and a variable name. Example:

    cajun/mustard

    I want to take that string and make it the variable name of that class type. It woule be implemented along the lines of:

    Code:
    string str;
    
    //read/process string here, get:
    str = "mustard";
    
    createName(str);                 //pass string to creator function
    When the function is called, I should get the variable:

    Class mustard;

    Thing is, I'm not supposed to know beforehand what the variable names are, only that I create them as they are read in. It could be mustard, it could be Maynard_James_Keenan, it could even be bazinga.

    My problem is, what do I do for createName()? I've looked into the concepts of pairing, Factory implementation, and maps, but I don't think they answer my question. I would greatly appreciate anyone's help.

    (P.S. A bonus question for the daring: if I run into the same variable name being read in twice, what steps can I take to make sure that a duplicate variable isn't created? Do I need to add in code, or does the compiler know to watch for multiple variables of the same name?)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you want to do this? Like, are you trying to write an interpreter for a very simplified version of C++?
    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

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    32
    essentially, i have a trie header file, a 10mb text file of compiled words and their syntactic labels (formatted like above), and a mission to make reading in the data, storing it, and getting to it as quickly and efficiently as possible. i want to create trie variables for each syntactic label, but there are so many of them, and not only do my references not list all the ones that are used, but declaring every single variable (i counted 35, excluding punctuation, which have their own tries) and assigning specific code to each one seems like the greatest waste of time and overhead imaginable for this scope.

    i know it seems like it may not be possible, but i'm just trying to see if i can dynamically create these variables to make my life a bit easier.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    From your description, a vector or map might be suitable.
    If you want to group on labels, then create a struct or class that represents a single group and variable and populate it with the information before putting it into your data structure.
    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.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by William Putnam View Post
    essentially, i have a trie header file, a 10mb text file of compiled words and their syntactic labels (formatted like above), and a mission to make reading in the data, storing it, and getting to it as quickly and efficiently as possible. i want to create trie variables for each syntactic label, but there are so many of them, and not only do my references not list all the ones that are used, but declaring every single variable (i counted 35, excluding punctuation, which have their own tries) and assigning specific code to each one seems like the greatest waste of time and overhead imaginable for this scope.
    I didn't know what a trie was until I looked it up at wiki:

    Trie - Wikipedia, the free encyclopedia

    However, I am familiar with associative arrays and in the case of hardware, content addressable memory.

    What is the goal here? Are you trying to search for variable names in order to return words or vice versa, or are you simply trying to search for a specific combination of word and variable name?

    It is possible to literally do what you're asking with some interactive languages such as APL (variable names and variables can be dynamically created from strings via "execute" (functions can be dynamically created from matrices), and a search could be performed from a list of variable names (which can be dynamically created by the "quad name list" operator), ... but I don't see what this would accomplish. You'd be stuck with APL's symbol search when trying to actually search for and use one of thousands of variables.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    32
    Quote Originally Posted by rcgldr View Post
    What is the goal here? Are you trying to search for variable names in order to return words or vice versa, or are you simply trying to search for a specific combination of word and variable name?
    In response to your question, my objective is to have the user enter a variable name and get a list of syntactical labels with which it's used. For example, doctor can be considered a noun and a verb, so it would search the trie of each syntactical label, and if it's found in the trie, the syntactical device is listed.

    I understand the concerns you may have that this is not the best method. However, I've already been playing with Flexarrays and binary search trees for my data structures class, and I really want to give tries a try. From what I've learned, tries are quick to search, and I'm looking for something that searches fast and efficiently, especially given how much data I'm reading in.

    I should've included a bit more context earlier, so I've attached the trie header/cpp file so you can see what it is I'm up against if that helps.
    trie.h
    trie.cpp

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by William Putnam View Post
    In response to your question, my objective is to have the user enter a variable name and get a list of syntactical labels with which it's used. For example, doctor can be considered a noun and a verb, so it would search the trie of each syntactical label, and if it's found in the trie, the syntactical device is listed.

    I understand the concerns you may have that this is not the best method.
    This is new to me. I don't have concerns about your choice, because I'm not aware of the possible methods. I'm not sure how a trie is optimally searched. If I get the time, I'll look at your code later.

    If the tree creation is a one time operation, and then the tree is searched repeatedly, I'm wondering if a something like a sorted flat array of structures that include strings and linkages might be better. Each structure could include the first 16 or 32 characters of the entry, then a link to more structures (with more links if needed). This way you could binary search the flat array part of your "dictionary".
    Last edited by rcgldr; 05-13-2013 at 12:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a Custom String Class
    By kisiellll in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2009, 11:31 AM
  2. Creating string from variable name
    By moddinati in forum C++ Programming
    Replies: 4
    Last Post: 06-17-2008, 12:48 PM
  3. I need help creating a string class
    By incognito in forum C++ Programming
    Replies: 1
    Last Post: 01-19-2002, 05:48 PM
  4. Creating a string class
    By incognito in forum C++ Programming
    Replies: 2
    Last Post: 01-19-2002, 05:40 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM