ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
David (name)
1 ( some variable )
bowler
2 (stats)
3
4
i can read the file in and store them into a 1d array however it stores each character in one element when i want to have each line in one element. please can any one help asap thanks
the code runs fine Hivemind, however i need it to be stored in a 1d array as i will need access to the elements, throughout the program. i need to make comparsions with the elements to other arrays i have
oh ok i see, sorry stupid question i am quite new to c++. to print out one line of the txt file would i have a counter then increment it with line? or how could the vector just point to a particular line in the text file
std::cout << "Read " << contents_of_file.size() << " lines from the file." << std::endl;
std::cout << "Any particular line is accessed using contents_of_file[line_number-1]."
<< std::endl;
If you read ten lines into the vector they have indexes 0-9. So if you want the first line, the index should be 0, the last the index would be nine, the fifth the index would 4. line number - 1...
I have a vector vector<string> the; set up in a class as public
then in another class what would i do to get access to it. would i have to use a pointer in the first class and then get reference to it?
public:
myObj.the;
myObjPtr->the[0];
/* class holding a vector with access method, only important bits shown */
class MyClass
public:
std::string get_element_in_vector(unsigned short index)
{
if(index > the_vector.size())
throw std::runtime_error("Index out of bounds");
return the_vector[index]; /* Only reached for valid indexes. */
}
private:
std::vector<std::string> the_vector;
};
Call it like you call any non-static member function. You can catch the return
value in const string reference if you dont need to modify it to speed up performance.
Code:
MyClass myinst;
/* Do stuff with myinst, like populating the vector. */
/* I dont need to modify str, so I catch it in a const reference */
const std::string& str = myinst.get_element_in_vector(some_valid_index);
/* str2 to I need to modify, so: */
std::string str2 = myinst.get_element_in_vector(another_valid_index);
i got two classes, 1 class has a vector where values are read in, i want a 2nd class to access the vector from class 1 to check the values in the a particlar element. the main cpp file will call these functions.
how would i go about this? i can't seem to access the vector in another class, and when i do it doesn't display anything. even though i have something in the vector
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.