473,790 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

templated std::vector variable as an argument by reference problem

sorry i am too sure how to write a more explicit subject but this code
doesn't compile for the type string and I am not sure why (and I am
not sure either how to describe the problem but by looking at the
program you should understand what I am trying to do easily, which is
convert an array of string to an array of another type, either string,
float or interger.)

I am not sure what I am trying to do is legal. Obvisouly it doesn't
seem to be as the compiler complains but I wonder if there's a way i
can get it to work ?

Thanks for your help.

-mark

template<typena me T>
void GetArray( std::vector<T&a rray )
{
std::vector<std ::stringstrArra y;
strArray.push_b ack( "test1" );
strArray.push_b ack( "test2" );

if ( typeid( std::string ) == typeid( T ) )
{

for ( size_t i = 0; i < strArray.size() ; ++i )
{
// DOESN'T WORK ??? <<< REFUSE TO COMPILE IF THE NEXT LINE IS
COMMENTED OUT!
//array.push_back ( strArray[i] );
}
}

std::vector<std ::stringintArra y;
intArray.push_b ack( "1" );
intArray.push_b ack( "2" );

if ( typeid( int ) == typeid( T ) )
{
for ( size_t i = 0; i < intArray.size() ; ++i )
{
array.push_back ( atoi( intArray[i].c_str() ) );
}
}

}

int main()
{
//std::vector<std ::stringstrArr;
//GetArray( strArr );

std::vector<int intArr;
GetArray<int>( intArr );

return 0;
}

/////

error: no matching function for call to 'std::vector<in t,
std::allocator< int::push_back( std::basic_stri ng<char,
std::char_trait s<char>, std::allocator< char&)'
/usr/include/c++/4.0.0/bits/stl_vector.h:60 2: note: candidates are:
void std::vector<_Tp , _Alloc>::push_b ack(const _Tp&) [with _Tp = int,
_Alloc = std::allocator< int>]

////

Mar 2 '07 #1
3 1856
hum clearly what i am trying to do in the code is completly stupid, as
of course if at runtime we call GetArray with a
std::vector<std ::stringargumen t the compiler still tries to make
sense of the code where numbers or pushed to this array. So it
compiles fines for numbers but then when i write the code for string
there's a mismatch (the same array can't be used to push string &
numbers).

Anyway... if anyone has in mind a mechanism that would allo me to do
that be great. Right now the only solution i see if to return a vector
of strings and the type of the token which the strings encode (strings
or number), then create a vector of that type (vector<intor
vector<stringan d call a function that converts an array of string to
an array of integers...

i was just hoping for something a bit smarter... ;-)

Mar 2 '07 #2
* ma*****@yahoo.c om:
sorry i am too sure how to write a more explicit subject but this code
doesn't compile for the type string and I am not sure why (and I am
not sure either how to describe the problem but by looking at the
program you should understand what I am trying to do easily, which is
convert an array of string to an array of another type, either string,
float or interger.)

I am not sure what I am trying to do is legal. Obvisouly it doesn't
seem to be as the compiler complains but I wonder if there's a way i
can get it to work ?

Thanks for your help.

-mark

template<typena me T>
void GetArray( std::vector<T&a rray )
{
std::vector<std ::stringstrArra y;
strArray.push_b ack( "test1" );
strArray.push_b ack( "test2" );

if ( typeid( std::string ) == typeid( T ) )
{

for ( size_t i = 0; i < strArray.size() ; ++i )
{
// DOESN'T WORK ??? <<< REFUSE TO COMPILE IF THE NEXT LINE IS
COMMENTED OUT!
//array.push_back ( strArray[i] );
}
}
The code above is compiled also for a type T that isn't std::string.

Instead of 'if' you can use a specialization of GetArray for type
std::string.

std::vector<std ::stringintArra y;
intArray.push_b ack( "1" );
intArray.push_b ack( "2" );

if ( typeid( int ) == typeid( T ) )
{
for ( size_t i = 0; i < intArray.size() ; ++i )
{
array.push_back ( atoi( intArray[i].c_str() ) );
}
}
The code above is compiled also for a type T that isn't integer.

Instead of 'if' and 'atoi' you can use a 'boost::lexical _cast' as a
generic implementation.

}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 2 '07 #3
you can use partial specialization.

ma*****@yahoo.c om wrote:
hum clearly what i am trying to do in the code is completly stupid, as
of course if at runtime we call GetArray with a
std::vector<std ::stringargumen t the compiler still tries to make
sense of the code where numbers or pushed to this array. So it
compiles fines for numbers but then when i write the code for string
there's a mismatch (the same array can't be used to push string &
numbers).

Anyway... if anyone has in mind a mechanism that would allo me to do
that be great. Right now the only solution i see if to return a vector
of strings and the type of the token which the strings encode (strings
or number), then create a vector of that type (vector<intor
vector<stringan d call a function that converts an array of string to
an array of integers...

i was just hoping for something a bit smarter... ;-)
Mar 2 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
7393
by: scooter | last post by:
what happens memory wise with this scenerio: std::vector<int> vecInt(20, 999); VecInt.assign(20, 0); do all the 999 values get overwritten or does the vector reassign the memory lso?
27
5982
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
0
1597
by: Jason Heyes | last post by:
I wrote a previous post that asked whether there was a reference-counted implementation of std::vector. Apparantly there wasn't. So my next question is, is it possible to write your own shared container that behaves like std::vector? Here is me trying to answer that question: class FooReference;
18
2881
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that contains another std::vector<CPosition>. Because one of the players is the client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a std::vector<CPlayer>::iterator "localplayer" that points to the...
17
3361
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is a transparent replacement for std:vector. The options seems to be:
32
69698
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: std::vector<int> fun(){ //build the vector v; return v; }
6
5655
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. char temp; std::vector<unsigned charmmessage; while (!done){
7
5714
by: digz | last post by:
template<typename _Tp, typename _Alloc> void vector<_Tp, _Alloc>:: _M_insert_aux(iterator __position, const _Tp& __x) { if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage) { this->_M_impl.construct(this->_M_impl._M_finish, *(this->_M_impl._M_finish - 1)); ++this->_M_impl._M_finish;
23
3484
by: Mike -- Email Ignored | last post by:
In std::vector, is reserve or resize required? On: Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri Jul 27 18:10:34 EDT 2007 i686 athlon i386 GNU/Linux Using: g++ (GCC) 4.1.2 20070502 (Red Hat 4.1.2-12) The program below fails, but if the reserve(en)
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10413
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4094
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.