1

I have to write something like

q.push_back(q1); ... to ... q.push_back(q100);

I am writing this as

for (int i = 1; i <= 100; i++) {
    try {
        std::string s = boost::lexical_cast<std::string > (i);
        "q.push_back(q" + s + ");";
    } catch (boost::bad_lexical_cast &) {
        std::cout << "Some error message \n";
    }
}

It compiles (no syntax errors) but does not work. I don't know how to mix cpp statements and strings to make a compound statement.

Any help here would be highly appreciated. Thanks!

7
  • 2
    It works perfectly. I just doesn't do what you want it to, but that's an entirely different matter. But to make sure I'm not misunderstanding you: You have a hundred variables q1, q2, q3, ... q100 and you want to put them into one vector, right? Commented Sep 19, 2011 at 15:44
  • "q.push_back(q" + s + ");"; what did you expect this string literal to do? Commented Sep 19, 2011 at 16:02
  • Yes I want a shortcut way to write q.push_back(q1); .. to q.push_back(q100); in the code. Commented Sep 19, 2011 at 16:03
  • @AJG85 - I kind of knew this would not work as this is just a string literal and not a statement. Commented Sep 19, 2011 at 16:06
  • @Ayesha You'll probably need a macro if you want to generate the code that does this. Commented Sep 19, 2011 at 16:08

6 Answers 6

1

How about:

for (int i = 1; i <= 100; i++)
{
    std::cout << "q.push_back(q" << i << ");\n";
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you're using std::string (which it looks like you are) try s.append(str);

Comments

0
std::ostringstream os;
for( int i = 1; i < 100; ++i)
{
   os << "s.push_back( q" << i << "); ";
}
std::string result = os.str();
// Do what you need with result

Your output string was put inside the for loop, so at each loop the string is initialized, the content generated in the previous loop was lost.

Comments

0

You aren't telling it where to put that string, it just creates a temporary. Try appending to s.

std::string s = "q.push_back(q";
s.append(boost::lexical_cast<std::string > (i));
s.append(");");

It isn't clear what you expect to do with the string. If you are printing it to std::cout, then your error message will get mixed in with the results. You should use std::cerr instead.

std::cerr << "Some error message" << std::endl;

Comments

0

The variables q1, q2, ..., q100 should have been an array in the first place.

Comments

0

If my understanding of what you are trying to do is correct, then you basically want to automatically generate some code. For that, people normally use simple macro definitions...

You can just define a macro that takes your respective integer from the for loop as a parameter and expands to the code you require with it...

Take a quick look at [ http://en.wikipedia.org/wiki/C_preprocessor#Token_concatenation ]. You can use ## in macros to glue things together, so you could use something like the following, inside a for loop for i:

#define MY_PUSH(vector, i) vector.push_back(q##i);

But as people have suggested, it might have been better to use an array of variables in the first place, as macros are basically evil for anything other than raw code generation tools...

3 Comments

This is exactly what I want, could you provide an example please? Thanks!
I haven't used a Macro before.
and you should aim at never using them, especially in a C++ environment, as they can get you in a lot more trouble than they can get you out...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.