•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 426,440 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,275 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2215 | Replies: 4
![]() |
•
•
Join Date: May 2005
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 1
Moderator edit: This response and its replies have been split from the C and C++ Timesaving Tips sticky because they do not add relevant material, and they are long enough to distract from the intended purpose of the thread. Further replies on this topic may be posted in this thread, while other useful time saving tips should still be directed to the sticky. -Narue
Well u can always use STL's "for_each" for efficiency, readability, portability & usability. Available in std::algorithm for any container types
Well u can always use STL's "for_each" for efficiency, readability, portability & usability. Available in std::algorithm for any container types
•
•
•
•
Originally Posted by Narue
Post your tips for making life easier in C and C++. I'll start:
Standard vector object initialization
The biggest problem with the standard vector class is that one can't use an array initializer. This forces us to do something like this:
Anyone who's had the rule of redundancy pounded into their head knows that the previous code could be wrapped in a loop:#include <iostream> #include <vector> using namespace std; int main() { vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(5); // Use the vector }
However, it's not terribly elegant, especially for a vector of complex types. So, Narue's first timesaving tip for C++ is to use a temporary array so that you can make use of an initializer. Because the vector class defines a constructor that takes a range of iterators, you can use the array to initialize your vector:#include <iostream> #include <vector> using namespace std; int main() { vector<int> v; for (int i = 1; i < 6; i++) v.push_back(i); // Use the vector }
#include <iostream> #include <vector> using namespace std; int main() { int a[] = {1,2,3,4,5}; vector<int> v(a, a + 5); // Use the vector }
•
•
Join Date: May 2005
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 1
It is always better to avoid '_' in names until unless u want the code to be terse or STL kindastuff..it also make code less readable than the smallCaptialized form.. :-|
•
•
•
•
Originally Posted by BountyX
I know this may not seem like a super time saving technique, but the way you name your variables will help save time and reduce compile time errors.. Having a conventional variable naming format will help keep things organized and easy to read.
Styles
Typically, most variable names get the every other word capitalized. Functions get the first letter of each word capitalized and constants get all of the letters capitalized.
Example:
Variable: First letter of every other word capitalized.Functions: First letter of each word capitalized .
- char theName[20];
Constants: All capitals
- void GetTheName();
Of course, sometimes choosing a good variable name can take a long time!
- #define THENAME 20
>Well u can always use STL's "for_each" for efficiency, readability, portability & usability.
for_each isn't nearly as useful as the propaganda suggests. Also, for_each would be a poor solution to the issue that this tip covers. A better solution would be generate_n, but that's still not as concise and easy to follow Consider:
How is that better? You said for_each though, so let's look at that one too. Well, for_each requires a range for the input sequence, so you still need a temporary list, and getting the function to add to a vector isn't as trivial as you seem to think. Instead of suggesting things at random, why don't you try implementing the suggestion first to see how reasonable it is?
>it also make code less readable than the smallCaptialized form..
That's your opinion, and it's debatable.
for_each isn't nearly as useful as the propaganda suggests. Also, for_each would be a poor solution to the issue that this tip covers. A better solution would be generate_n, but that's still not as concise and easy to follow Consider:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
class increment {
int _i;
public:
increment(int start = 0): _i(start) {}
int operator()() { return _i++; }
};
int main()
{
vector<int> v;
generate_n(back_inserter(v), 5, increment(1));
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout<<endl;
cin.get();
}>it also make code less readable than the smallCaptialized form..
That's your opinion, and it's debatable.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
•
•
Join Date: May 2005
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 1
Prototype for 'for_each':
template<class InputIterator, class Function> inline
Function for_each(InputIterator first, InputIterator last, Function F)
Following code does the insertion of integer 0 onwards in vector<int> of size 5 and prints them
void displayData(int data)
{
cout << data << "\n";
}
void setData(int& data)
{
static int i = 0;
data = i++;
}
void main()
{
std::vector<int> vecString(5);
std::for_each(vecString.begin(),vecString.end(),setData);
std::for_each(vecString.begin(),vecString.end(),displayData);
cout << "\n";
}
Output :
0
1
2
3
4
>How is that better?
Judge it by running & analyzing things
>Well, for_each requires a range for the input sequence, so you still need a >temporary list
Well why do u need a temporary list..iterators are the only thing that is required. And if u r well versed with STLs iterator are always there for any kind of container & even u can design ur iterators for ex. iterator for binary search tree etc. not present in STL.
>and getting the function to add to a vector isn't as trivial as you seem to >think.
I still say its trivial u can replace just the setData() function written above to change values to be inserted..for example reading it from file.
>Instead of suggesting things at random, why don't you try implementing the suggestion first to see how reasonable it is?
Instead of passing conclusion without going into details, why don't u listen to everybody, analyze things and make statement if its required.
I don't want to offend anybody..what we need is broader horizon to look things, may be we all learn something..excuse if my remark hurt anybody.
"Knowledge is free, share it to grow it"
template<class InputIterator, class Function> inline
Function for_each(InputIterator first, InputIterator last, Function F)
Following code does the insertion of integer 0 onwards in vector<int> of size 5 and prints them
void displayData(int data)
{
cout << data << "\n";
}
void setData(int& data)
{
static int i = 0;
data = i++;
}
void main()
{
std::vector<int> vecString(5);
std::for_each(vecString.begin(),vecString.end(),setData);
std::for_each(vecString.begin(),vecString.end(),displayData);
cout << "\n";
}
Output :
0
1
2
3
4
>How is that better?
Judge it by running & analyzing things
>Well, for_each requires a range for the input sequence, so you still need a >temporary list
Well why do u need a temporary list..iterators are the only thing that is required. And if u r well versed with STLs iterator are always there for any kind of container & even u can design ur iterators for ex. iterator for binary search tree etc. not present in STL.
>and getting the function to add to a vector isn't as trivial as you seem to >think.
I still say its trivial u can replace just the setData() function written above to change values to be inserted..for example reading it from file.
>Instead of suggesting things at random, why don't you try implementing the suggestion first to see how reasonable it is?
Instead of passing conclusion without going into details, why don't u listen to everybody, analyze things and make statement if its required.
I don't want to offend anybody..what we need is broader horizon to look things, may be we all learn something..excuse if my remark hurt anybody.
"Knowledge is free, share it to grow it"
•
•
•
•
Originally Posted by Narue
>Well u can always use STL's "for_each" for efficiency, readability, portability & usability.
for_each isn't nearly as useful as the propaganda suggests. Also, for_each would be a poor solution to the issue that this tip covers. A better solution would be generate_n, but that's still not as concise and easy to follow Consider:
How is that better? You said for_each though, so let's look at that one too. Well, for_each requires a range for the input sequence, so you still need a temporary list, and getting the function to add to a vector isn't as trivial as you seem to think. Instead of suggesting things at random, why don't you try implementing the suggestion first to see how reasonable it is?#include <algorithm> #include <iostream> #include <iterator> #include <vector> using namespace std; class increment { int _i; public: increment(int start = 0): _i(start) {} int operator()() { return _i++; } }; int main() { vector<int> v; generate_n(back_inserter(v), 5, increment(1)); copy(v.begin(), v.end(), ostream_iterator<int>(cout, " ")); cout<<endl; cin.get(); }
>it also make code less readable than the smallCaptialized form..
That's your opinion, and it's debatable.
Okay, first, quotes go before responses. This makes it easier to figure out what response goes with what quote. Second, don't quote everything. That just makes the thread longer. Instead, pick out relevant statements and quote them.
>Prototype for 'for_each':
I'm quite familiar with for_each, so you don't need to tell me what it is.
>Judge it by running & analyzing things
Well, just by looking: It doesn't give the right output (0-4 instead of 1-5), you fail to include the requisite headers, you fail to qualify cout with std, you have to write two tiny functions to get for_each to work properly (thus displaying one of for_each's drawbacks), you have to make sure that vecString is properly sized from the start, vecString is a poor name, you've introduced an unnecessary static variable, and main doesn't return void (it returns int).
So I ask again, how is your use of for_each better than this, which is a better fit for your solution:
>Well why do u need a temporary list
To get the values. The list is either explicit through an array or other data structure, or implicit through a generator function or function object.
>..iterators are the only thing that is required
Um, do you even know what an iterator is?
>I still say its trivial
Then you're wrong. I said "add to a vector", not "assign to a vector". I'm talking about starting with an empty vector and using for_each to actually insert values into it, much like you would with copy:
Any idiot can write a function to assign values to an existing vector and use for_each. That's not a challenge at all, and it doesn't address the initial problem that the tip covers.
>why don't u listen to everybody, analyze things and make statement
I listened to what you said, analyzed your solution, and deemed it inappropriate. Thus my statements. Perhaps you should get your ego in check and try to understand what I'm trying to teach you. And yes, I am trying to teach you since your code suggests a lack of fundamental understanding about C++ in general and your comments display only a remedial understanding of the "STL".
>Prototype for 'for_each':
I'm quite familiar with for_each, so you don't need to tell me what it is.
>Judge it by running & analyzing things
Well, just by looking: It doesn't give the right output (0-4 instead of 1-5), you fail to include the requisite headers, you fail to qualify cout with std, you have to write two tiny functions to get for_each to work properly (thus displaying one of for_each's drawbacks), you have to make sure that vecString is properly sized from the start, vecString is a poor name, you've introduced an unnecessary static variable, and main doesn't return void (it returns int).
So I ask again, how is your use of for_each better than this, which is a better fit for your solution:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
int a[] = {1,2,3,4,5};
std::vector<int> v;
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, "\n"));
}To get the values. The list is either explicit through an array or other data structure, or implicit through a generator function or function object.
>..iterators are the only thing that is required
Um, do you even know what an iterator is?
>I still say its trivial
Then you're wrong. I said "add to a vector", not "assign to a vector". I'm talking about starting with an empty vector and using for_each to actually insert values into it, much like you would with copy:
int a[] = {1,2,3,4,5};
std::vector<int> v;
std::copy(a, a + 5, std::back_inserter(v));>why don't u listen to everybody, analyze things and make statement
I listened to what you said, analyzed your solution, and deemed it inappropriate. Thus my statements. Perhaps you should get your ego in check and try to understand what I'm trying to teach you. And yes, I am trying to teach you since your code suggests a lack of fundamental understanding about C++ in general and your comments display only a remedial understanding of the "STL".
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- memory management in wndows 2000 (Windows NT / 2000 / XP / 2003)
- C and C++ Timesaving Tips (C++)
- Shut Down And Saving Your Settings (Windows NT / 2000 / XP / 2003)
- Creating a disk image (OS X)
- Slow Internet Explorer 6 Response Time- HJT log (Viruses, Spyware and other Nasties)
- Slow Internet Explorer 6 Response Time (Windows NT / 2000 / XP / 2003)
- For Sale: Games and Books and Others (Geeks' Lounge)
- Annual Web Hosting (Web Hosting Deals)
Other Threads in the C++ Forum
- Previous Thread: OpenSSL using C++.
- Next Thread: erge and Heap...which is really faster



Linear Mode