Hi,
Is it possible to add an int or a string or a char to a vector variable??
For example if I declare a vector variable as follows:
vector<string> abc;
Can I append an integer value or some string value to the vector variable such that every time I call a function that has this declaration, it creates a new vector variable abc appened with the int or string value?
Is there any other method by which I can do above operation
Please help.
Thanks,
Rishi
5 3077
i dont think tht u can append the vector variable like tht
but if i m able to guess ur functionality then u can be better using array of vectors or vector of vectors. Then u can access the sub(contained) vectors like a[0], a[1] and so on. If u can clarify more on ur problem (or requirement) then may be we can help further.
If I understand you correctly, you should be able to write a function template to do that: -
template< typename T>
-
vector<T>* MyFunction(T& arg)
-
{
-
vector<T>* temp = new vector<T>;
-
temp->push_back(arg);
-
return temp;
-
}
-
int main()
-
{
-
int i = 30;
-
vector<int>* val = MyFunction(i);
-
string str("Hello");
-
vector<string>* val1 = MyFunction(str);
-
}
-
If I misunderstand, my apologies.
Hi,
My exact problem is clearly defined below:
Please look at the following code:
dependencies(int i)
{
string str;
vector<string> fd;
cout<<"enter the attributes of fd"<<i<<endl;
cin>>str;
fd.push_back(str);
}
int _tmain(int argc, _TCHAR* argv[])
{
int num;
cout<<"Enter the number of functionaldependencies"<<endl;
cin>>num;
for(int i=0; i<num; i++)
{
dependencies(i);
}
return 0;
}
In the above code I am calling the function "dependencies" depending on the given input for number of functional dependencies. In the "dependencies" function, whatever i am inputting is pushed into the vector. But everytime I call the function, the input that I give gets into the same vector.
But what i want to do is, everytime I call the function dependencies, it should create a new vector and push that iteration inputs into new vector instead of pushing into the same old vector.
Is it possible to do that way??
Please help!!
Thanks!!
Take a close look at your dependencies function. Each time you call it, it will create a new and "local" vector of strings. Since it is local, each time you leave dependencies, the vector and its values are deleted. I doubt this is what you want.
Now take a look at MyFunction in post #3. It creates a new vector on the "heap" each time it is called. It returns a pointer to that vector so that the calling program can use the vector. I think this is what you want.
Add your loop to MyFunction or add the vector creation to dependencies and return the new vector.
flavourofbru,
I noticed you had posted a new thread containing this very same question. Please limit your question to one thread in the future - there's no need to have 2 discussions happening at once on the same subject!
Thanks a lot.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Forecast |
last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully.
: // proj2.cc: returns a dynamic vector and prints out at main~~
: //
: #include <iostream>
: #include <vector>
:
: using...
|
by: Alex |
last post by:
I'm looking for a fast way to split a string into individual tokens.
The typical format of the string is token1|token2|token3|...|tokenN|
and the number of tokens varies (which is why i use a...
|
by: Jeannie |
last post by:
Hello group!
I'm in Europe, traveling with my laptop, and I don't any compilers
other than Borland C++ 5.5. available. I also don't have any manuals
or help files available. Sadly, more...
|
by: Hp |
last post by:
Hi All,
Thanks a lot for all your replies.
My requirement is as follows:
I need to read a text file, eliminate certain special characters(like !
, - = + ), and then convert it to lower case and...
|
by: toton |
last post by:
Hi,
I want to append one vector after another.
so like,
vector<intv1; ///put some elements to v2.
I have a second vector
vector<intv2; ///it has some elements.
Now I do
v1.reserve(v1.size()...
|
by: Shafik |
last post by:
Hello,
I am new to C++. I know the reason is probably template instantiation
problems ... but what's the *real* reason I cannot declare a:
vector<stringv = vector<string>(4);
Thanks!...
|
by: WP |
last post by:
Hello! I need some help with my program...it's supposed to read infix
expressions line by line from stdin and each expression should be
divided into operands and operators and added to a vector of...
|
by: kungfuelmosan |
last post by:
Hey guys, Im just getting into c++ at the moment so please bare with
me
Basically i need to declare a vector<stringstringArray(50)
inside a class, however by doing so i am getting the following...
|
by: barcaroller |
last post by:
1. If I pass pointers (char*) as iterators to an STL algorithm and the
return value is an iterator, can I convert that iterator to a pointer? If
yes, how?
2. What is the internal...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |