本文整理汇总了C++中forward_list类的典型用法代码示例。如果您正苦于以下问题:C++ forward_list类的具体用法?C++ forward_list怎么用?C++ forward_list使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了forward_list类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: while
forward_list<int> LinkedListsExpert::partition(forward_list<int> list, int x){
forward_list<int> lessThanX, greaterThanX, equalX;
forward_list<int>::iterator it = list.begin();
while (it!=list.end()) {
if (*it < x) {
lessThanX.push_front(*it);
} else if (*it > x){
greaterThanX.push_front(*it);
} else{
equalX.push_front(*it);
}
it++;
}
//merge lists
it = equalX.begin();
while (it != equalX.end()) {
greaterThanX.push_front(*it);
it++;
}
it = lessThanX.begin();
while (it != lessThanX.end()) {
greaterThanX.push_front(*it);
it++;
}
return greaterThanX;
}
示例2: printList
void LinkedListsExpert::printList(forward_list<int> list){
std::forward_list<int>::iterator it;
for (it = list.begin(); it != list.end(); it++) {
cout << *it << " ";
}
cout << "\n";
}
示例3: primeCount
int primeCount(forward_list<int> lst)
{
int total;
if(lst.empty())
{
return 0;
}
else
{
bool prime = isPrime(lst.front());
lst.pop_front();
if(prime == true)
{
total = 1 + primeCount(lst);
}
if(prime == false)
{
total = primeCount(lst);
}
}
return total;
}
示例4: listCopy
void listCopy(forward_list<T> L, forward_list<T> &P) // reverse L and put it into P
{
for(typename forward_list<T>::iterator start = L.begin() ; start != L.end() ; ++start)
{
P.push_front(*start);
}
}
示例5: display
void display(forward_list<T> lst)
{
for(typename forward_list<T>::iterator start = lst.begin(); start != lst.end() ; ++start)
{
cout << *start << " ";
}
cout << endl;
}
示例6: printList
template <typename T> void printList(const forward_list<T> &listRef)
{
if (listRef.empty())
cout<<"List is empty";
else
{
ostream_iterator<T> output(cout, " ");
copy(listRef.begin(), listRef.end(), output);
}
}
示例7: remove_evens_and_double_odds
void remove_evens_and_double_odds(forward_list<int>& data) {
for (auto cur = data.begin(), prev = data.before_begin(); cur != data.end(); ) {
if (*cur & 0x1)
cur = data.insert_after(prev, *cur),
advance(prev, 2),
advance(cur, 2);
else
cur = data.erase_after(prev);
}
}
示例8: insertWord
void insertWord(forward_list<string> &forList,
const string &str1,const string &str2)
{
auto prev = forList.before_begin();
for(auto i=forList.begin();i != forList.end();prev=i,++i)
if(*i == str1){
forList.insert_after(i,str2);
return ;
}
forList.insert_after(prev,str2);
}
示例9: primeCount
int primeCount(forward_list<int> lst){
if(lst.empty()){
return 0;
}else{
bool prime = isPrime(lst.front());
lst.pop_front();
if(prime){
return 1 + primeCount(lst);
}else{
return primeCount(lst);
}
}
}
示例10: find_and_insert
void find_and_insert(forward_list<string> &list, string const& to_find, string const& to_add)
{
auto prev = list.before_begin();
for (auto curr = list.begin(); curr != list.end(); prev = curr++)
{
if (*curr == to_find)
{
list.insert_after(curr, to_add);
return;
}
}
list.insert_after(prev, to_add);
}
示例11: merge
// Merge a connected compontent of lines to a new line with width.
void merge(const forward_list<LineSeg*>& component, LineSegW& out) {
// TODO: in principle no loop/collection is necessary
// Note: profiling indicates this is not even close to a bottleneck, so ok for now
forward_list<Point*> component2i;
for (auto vec = component.begin(); vec != component.end(); ++vec) {
component2i.push_front(&(*vec)->s);
component2i.push_front(&(*vec)->e);
//component2i.push_front((Vec2i*)&(*vec)->val[0]);
//component2i.push_front((Vec2i*)&(*vec)->val[2]);
}
line_fit(component2i, out);
}
示例12: primeCount
int primeCount( forward_list<int> lst ){
static int counter = 0;
if( lst.empty() ) { return 0; }// If lst is empty, then counter = 0.
else
{
if ( isPrime( lst.front() ) ){ ++counter;}
}
lst.pop_front();
primeCount(lst);
return counter;
} // Function that returns number of Primes in a forward_list
示例13: func
void func(forward_list<string> &flist, const string s1, const string s2)
{
auto before = flist.before_begin();
auto curr = flist.begin();
while(curr!=flist.end()) {
if(*curr == s1){
flist.insert_after(curr, s2);
return;
}else{
curr++;
}
}
flist.insert_after(curr, s2);
}
示例14: primeCount
int primeCount(forward_list<int> lst){
if(lst.empty()){ //base case
return 0; //empty list has no (prime) numbers
}
if(isPrime(lst.front())){ //check if first item is prime
cout << lst.front() << endl;
lst.pop_front(); //can do this bc lst is passed by value
return 1 + primeCount(lst); //recursive call
}
else{ //first number isnt prime
lst.pop_front(); //dont output, just delete it
return primeCount(lst); //dont add 1 to primecount
}
}
示例15: Insert
void Insert(forward_list<string> &lst, const string &str1, const string &str2) {
auto prev = lst.cbefore_begin();
auto curr = lst.cbegin();
while (curr != lst.cend()) {
if (*curr != str1) {
prev = curr;
++curr;
}
else {
lst.insert_after(curr, str2);
return;
}
}
lst.insert_after(prev, str2);
}