473,320 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

appending string or int to vector variable

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
Sep 10 '07 #1
5 3077
alind
7
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.
Sep 10 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
If I understand you correctly, you should be able to write a function template to do that:

Expand|Select|Wrap|Line Numbers
  1. template< typename T>
  2. vector<T>* MyFunction(T& arg)
  3. {
  4.     vector<T>* temp = new vector<T>;
  5.     temp->push_back(arg);
  6.     return temp;
  7. }
  8. int main()
  9. {
  10.     int i = 30;
  11.     vector<int>* val = MyFunction(i);
  12.     string str("Hello");
  13.     vector<string>* val1 = MyFunction(str);
  14. }
  15.  
If I misunderstand, my apologies.
Sep 10 '07 #3
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!!
Sep 10 '07 #4
RRick
463 Expert 256MB
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.
Sep 11 '07 #5
Ganon11
3,652 Expert 2GB
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.
Sep 11 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
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...
10
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...
15
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...
12
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...
5
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()...
10
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!...
3
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...
4
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...
9
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...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
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...
0
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...
0
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...
0
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.